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
-
#data ⇒ Hash
The hash built from JSON document.
-
#doc ⇒ Hash
The document hash.
-
#id ⇒ String
The identificator of the document.
-
#key ⇒ Object
The key which was emitted by map function.
-
#meta ⇒ Hash
The meta data linked to the document.
-
#spatial ⇒ Array<View>
The list of spatial views defined or empty array.
-
#value ⇒ Object
The value which was emitted by map function.
-
#views ⇒ Array<View>
The list of views defined or empty array.
Class Method Summary collapse
-
.wrap(bucket, data) ⇒ ViewRow
Wraps data hash into ViewRow instance.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get attribute of the document.
-
#[]=(key, value) ⇒ Object
Set document attribute.
-
#design_doc? ⇒ true, false
Check if the document is design.
-
#has_key?(key) ⇒ true, false
Check attribute existence.
-
#has_views? ⇒ true, false
Check if the document has views defines.
-
#initialize(bucket, data) ⇒ ViewRow
constructor
Initialize the document instance.
- #inspect ⇒ Object
Constructor Details
#initialize(bucket, data) ⇒ ViewRow
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 138 139 140 141 142 143 144 145 |
# 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 <<-EOV, __FILE__, __LINE__ + 1 def #{name}(params = {}) View.new(@bucket, "\#{@id}/_view/#{name}", params) end EOV end end if @doc.has_key?('spatial') @doc['spatial'].each do |name, _| @spatial << name self.instance_eval <<-EOV, __FILE__, __LINE__ + 1 def #{name}(params = {}) View.new(@bucket, "\#{@id}/_spatial/#{name}", params) end EOV end end end end |
Instance Attribute Details
#data ⇒ Hash
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 |
#doc ⇒ Hash
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 |
#id ⇒ String
The identificator of the document
78 79 80 |
# File 'lib/couchbase/view_row.rb', line 78 def id @id end |
#key ⇒ Object
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 |
#meta ⇒ Hash
The meta data linked to the document
85 86 87 |
# File 'lib/couchbase/view_row.rb', line 85 def @meta end |
#spatial ⇒ Array<View>
The list of spatial views defined or empty array
99 100 101 |
# File 'lib/couchbase/view_row.rb', line 99 def spatial @spatial end |
#value ⇒ Object
The value which was emitted by map function
62 63 64 |
# File 'lib/couchbase/view_row.rb', line 62 def value @value end |
#views ⇒ Array<View>
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
Instance Method Details
#[](key) ⇒ Object
Get attribute of the document
Fetches attribute from underlying document hash
171 172 173 |
# File 'lib/couchbase/view_row.rb', line 171 def [](key) @doc[key] end |
#[]=(key, value) ⇒ Object
Set document attribute
Set or update the attribute in the document hash
197 198 199 |
# File 'lib/couchbase/view_row.rb', line 197 def []=(key, value) @doc[key] = value end |
#design_doc? ⇒ true, false
Check if the document is design
206 207 208 |
# File 'lib/couchbase/view_row.rb', line 206 def design_doc? !!(@doc && @id =~ %r(_design/)) end |
#has_key?(key) ⇒ true, false
Check attribute existence
183 184 185 |
# File 'lib/couchbase/view_row.rb', line 183 def has_key?(key) @doc.has_key?(key) end |
#has_views? ⇒ true, false
Check if the document has views defines
217 218 219 |
# File 'lib/couchbase/view_row.rb', line 217 def has_views? !!(design_doc? && !@views.empty?) end |
#inspect ⇒ Object
221 222 223 224 225 226 227 228 |
# File 'lib/couchbase/view_row.rb', line 221 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 |