Class: Couchbase::ViewRow

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

Overview

This class encapsulates structured JSON document

It behaves like Hash for document included into row, and has access methods to row data as well.

Direct Known Subclasses

DesignDoc

Constant Summary

Constants included from Constants

Constants::S_CAS, Constants::S_DOC, Constants::S_FLAGS, Constants::S_ID, Constants::S_IS_LAST, Constants::S_KEY, Constants::S_META, Constants::S_VALUE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, data) ⇒ ViewRow

Initialize the document instance

It takes reference to the bucket, data hash.

Since:

  • 1.2.0



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/couchbase/view_row.rb', line 136

def initialize(bucket, data)
  @bucket = bucket
  @data   = data
  @key    = data.key
  @last   = false
  @id     = data.id unless data.is_a?(ViewRowReduced)

  case data
  when ViewRowWithDocs, SpatialViewRowWithDocs
    @doc      = data.document
  when ViewRowReduced
    @value    = MultiJson.load(data.value)
  when SpatialViewRowNoDocs, SpatialViewRowWithDocs
    @geometry = data.geometry
    @bbox     = data.bbox
  end
end

Instance Attribute Details

#dataHash

The hash built from JSON document.

This is complete response from the Couchbase

Since:

  • 1.2.0



81
82
83
# File 'lib/couchbase/view_row.rb', line 81

def data
  @data
end

#docHash

The document hash.

It usually available when view executed with :include_doc argument.

Since:

  • 1.2.0



111
112
113
# File 'lib/couchbase/view_row.rb', line 111

def doc
  @doc
end

#idString

The identificator of the document

Since:

  • 1.2.0



118
119
120
# File 'lib/couchbase/view_row.rb', line 118

def id
  @id
end

#keyObject

The key which was emitted by map function

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



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

def key
  @key
end

#metaHash

The meta data linked to the document

Since:

  • 1.2.0



125
126
127
# File 'lib/couchbase/view_row.rb', line 125

def meta
  @meta
end

#valueObject

The value which was emitted by map function



102
103
104
# File 'lib/couchbase/view_row.rb', line 102

def value
  @value
end

Class Method Details

.wrap(bucket, data) ⇒ ViewRow

Wraps data hash into ViewRow instance

See Also:

Since:

  • 1.2.0



165
166
167
# File 'lib/couchbase/view_row.rb', line 165

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

Instance Method Details

#[](key) ⇒ Object

Get attribute of the document

Fetches attribute from underlying document hash

Since:

  • 1.2.0



178
179
180
# File 'lib/couchbase/view_row.rb', line 178

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

#[]=(key, value) ⇒ Object

Set document attribute

Set or update the attribute in the document hash

Since:

  • 1.2.0



204
205
206
# File 'lib/couchbase/view_row.rb', line 204

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

#has_key?(key) ⇒ true, false

Check attribute existence

Since:

  • 1.2.0



190
191
192
# File 'lib/couchbase/view_row.rb', line 190

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

#inspectObject

Since:

  • 1.2.0



217
218
219
220
221
222
223
224
# File 'lib/couchbase/view_row.rb', line 217

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

#last?true, false

Signals if this row is last in a stream

Since:

  • 1.2.1



213
214
215
# File 'lib/couchbase/view_row.rb', line 213

def last?
  @last
end