Class: Couchbase::ViewRow

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/view_row.rb

Overview

This class encapsulates structured JSON document

It behaves like Hash, but also defines special methods for each view if the documnent considered as Design document.

See Also:

Since:

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ViewRow) initialize(bucket, data)

Initialize the document instance

It takes reference to the bucket, data hash. It will define view methods if the data object looks like design document.

Parameters:

  • bucket (Couchbase::Bucket)

    the reference to connection

  • data (Hash)

    the data hash, which was built from JSON document representation

Since:

  • 1.2.0



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/couchbase/view_row.rb', line 111

def initialize(bucket, data)
  @bucket = bucket
  @data = data
  @key = data['key']
  @value = data['value']
  if data['doc']
    @meta = data['doc']['meta']
    @doc = data['doc']['value']
  end
  @id = data['id'] || @meta && @meta['id']
  @views = []
  @spatial = []
  if design_doc?
    if @doc.has_key?('views')
      @doc['views'].each do |name, _|
        @views << name
        self.instance_eval "def \#{name}(params = {})\nView.new(@bucket, \"\\\#{@id}/_view/\#{name}\", params)\nend\n", __FILE__, __LINE__ + 1
      end
    end
    if @doc.has_key?('spatial')
      @doc['spatial'].each do |name, _|
        @spatial << name
        self.instance_eval "def \#{name}(params = {})\nView.new(@bucket, \"\\\#{@id}/_spatial/\#{name}\", params)\nend\n", __FILE__, __LINE__ + 1
      end
    end
  end
end

Instance Attribute Details

- (Hash) data

The hash built from JSON document.

This is complete response from the Couchbase

Returns:

  • (Hash)

Since:

  • 1.2.0



41
42
43
# File 'lib/couchbase/view_row.rb', line 41

def data
  @data
end

- (Hash) doc

The document hash.

It usually available when view executed with :include_doc argument.

Returns:

  • (Hash)

Since:

  • 1.2.0



71
72
73
# File 'lib/couchbase/view_row.rb', line 71

def doc
  @doc
end

- (String) id

The identificator of the document

Returns:

  • (String)

Since:

  • 1.2.0



78
79
80
# File 'lib/couchbase/view_row.rb', line 78

def id
  @id
end

- (Object) key

The key which was emitted by map function

Usually it is String (the object _id) but it could be also any compount JSON value.



53
54
55
# File 'lib/couchbase/view_row.rb', line 53

def key
  @key
end

- (Hash) meta

The meta data linked to the document

Returns:

  • (Hash)

Since:

  • 1.2.0



85
86
87
# File 'lib/couchbase/view_row.rb', line 85

def meta
  @meta
end

- (Array<View>) spatial

The list of spatial views defined or empty array

Returns:

Since:

  • 1.2.0



99
100
101
# File 'lib/couchbase/view_row.rb', line 99

def spatial
  @spatial
end

- (Object) value

The value which was emitted by map function



62
63
64
# File 'lib/couchbase/view_row.rb', line 62

def value
  @value
end

- (Array<View>) views

The list of views defined or empty array

Returns:

Since:

  • 1.2.0



92
93
94
# File 'lib/couchbase/view_row.rb', line 92

def views
  @views
end

Class Method Details

+ (ViewRow) wrap(bucket, data)

Wraps data hash into ViewRow instance

Parameters:

  • bucket (Couchbase::Bucket)

    the reference to connection

  • data (Hash)

    the data hash, which was built from JSON document representation

Returns:

See Also:

Since:

  • 1.2.0



160
161
162
# File 'lib/couchbase/view_row.rb', line 160

def self.wrap(bucket, data)
  ViewRow.new(bucket, data)
end

Instance Method Details

- (Object) [](key)

Get attribute of the document

Fetches attribute from underlying document hash

Parameters:

  • key (String)

    the attribute name

Returns:

  • (Object)

    property value or nil

Since:

  • 1.2.0



173
174
175
# File 'lib/couchbase/view_row.rb', line 173

def [](key)
  @doc[key]
end

- (Object) []=(key, value)

Set document attribute

Set or update the attribute in the document hash

Parameters:

  • key (String)

    the attribute name

  • value (Object)

    the attribute value

Returns:

  • (Object)

    the value

Since:

  • 1.2.0



199
200
201
# File 'lib/couchbase/view_row.rb', line 199

def []=(key, value)
  @doc[key] = value
end

- (true, false) design_doc?

Check if the document is design

Returns:

  • (true, false)

Since:

  • 1.2.0



208
209
210
# File 'lib/couchbase/view_row.rb', line 208

def design_doc?
  !!(@doc && @id =~ %r(_design/))
end

- (true, false) has_key?(key)

Check attribute existence

Parameters:

  • key (String)

    the attribute name

Returns:

  • (true, false)

    true if the given attribute is present in in the document.

Since:

  • 1.2.0



185
186
187
# File 'lib/couchbase/view_row.rb', line 185

def has_key?(key)
  @doc.has_key?(key)
end

- (true, false) has_views?

Check if the document has views defines

Returns:

  • (true, false)

    true if the document have views

See Also:

Since:

  • 1.2.0



219
220
221
# File 'lib/couchbase/view_row.rb', line 219

def has_views?
  !!(design_doc? && !@views.empty?)
end

- (Object) inspect

Since:

  • 1.2.0



223
224
225
226
227
228
229
230
# File 'lib/couchbase/view_row.rb', line 223

def inspect
  desc = "#<#{self.class.name}:#{self.object_id} "
  desc << [:@id, :@key, :@value, :@doc, :@meta, :@views].map do |iv|
    "#{iv}=#{instance_variable_get(iv).inspect}"
  end.join(' ')
  desc << ">"
  desc
end