Class: Couchbase::ViewRow
- Inherits:
-
Object
- Object
- Couchbase::ViewRow
- 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.
Instance Attribute Summary (collapse)
-
- (Hash) data
The hash built from JSON document.
-
- (Hash) doc
The document hash.
-
- (String) id
The identificator of the document.
-
- (Object) key
The key which was emitted by map function.
-
- (Hash) meta
The meta data linked to the document.
-
- (Array<View>) spatial
The list of spatial views defined or empty array.
-
- (Object) value
The value which was emitted by map function.
-
- (Array<View>) views
The list of views defined or empty array.
Class Method Summary (collapse)
-
+ (ViewRow) wrap(bucket, data)
Wraps data hash into ViewRow instance.
Instance Method Summary (collapse)
-
- (Object) [](key)
Get attribute of the document.
-
- (Object) []=(key, value)
Set document attribute.
-
- (true, false) design_doc?
Check if the document is design.
-
- (true, false) has_key?(key)
Check attribute existence.
-
- (true, false) has_views?
Check if the document has views defines.
-
- (ViewRow) initialize(bucket, data)
constructor
Initialize the document instance.
- - (Object) inspect
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.
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'] = data['doc']['meta'] @doc = data['doc']['value'] end @id = data['id'] || && ['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
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.
71 72 73 |
# File 'lib/couchbase/view_row.rb', line 71 def doc @doc end |
- (String) id
The identificator of the document
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
85 86 87 |
# File 'lib/couchbase/view_row.rb', line 85 def end |
- (Array<View>) spatial
The list of spatial views defined or empty array
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
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
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
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
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
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
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
219 220 221 |
# File 'lib/couchbase/view_row.rb', line 219 def has_views? !!(design_doc? && !@views.empty?) end |
- (Object) inspect
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 |