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.
-
- (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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/couchbase/view_row.rb', line 104 def initialize(bucket, data) @bucket = bucket @data = data @key = data['key'] @value = data['value'] @doc = data['doc'] @id = if @value.is_a?(Hash) && @value['_id'] @value['_id'] else @data['id'] end @views = [] if design_doc? && @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 @doc.keys.each do |key| if %w($flags $expiration $cas).include?(key) [key.sub(/^\$/, '')] = @doc.delete(key) 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 |
- (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
148 149 150 |
# File 'lib/couchbase/view_row.rb', line 148 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
161 162 163 |
# File 'lib/couchbase/view_row.rb', line 161 def [](key) @doc[key] end |
- (Object) []=(key, value)
Set document attribute
Set or update the attribute in the document hash
187 188 189 |
# File 'lib/couchbase/view_row.rb', line 187 def []=(key, value) @doc[key] = value end |
- (true, false) design_doc?
Check if the document is design
196 197 198 |
# File 'lib/couchbase/view_row.rb', line 196 def design_doc? !!(@doc && @id =~ %r(_design/)) end |
- (true, false) has_key?(key)
Check attribute existence
173 174 175 |
# File 'lib/couchbase/view_row.rb', line 173 def has_key?(key) @doc.has_key?(key) end |
- (true, false) has_views?
Check if the document has views defines
207 208 209 |
# File 'lib/couchbase/view_row.rb', line 207 def has_views? !!(design_doc? && !@views.empty?) end |
- (Object) inspect
211 212 213 214 215 216 217 218 |
# File 'lib/couchbase/view_row.rb', line 211 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 |