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.

Parameters:

  • bucket (Couchbase::Bucket) —

    the reference to connection

  • data (Hash) —

    the data hash, which was built from JSON document representation

Since:

  • 1.2.0



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/couchbase/view_row.rb', line 122

def initialize(bucket, data)
  @bucket = bucket
  @data   = data
  @key    = data.key
  @value  = data.value
  @id     = data.id
  @last   = false

  case data
  when ViewRowWithDocs, SpatialViewRowWithDocs
    @doc   = data.document
  when SpatialViewRowNoDocs, SpatialViewRowWithDocs
    @geometry = data.geometry
    @bbox     = data.bbox
  end
end

Instance Attribute Details

#data ⇒ Hash

The hash built from JSON document.

This is complete response from the Couchbase

Returns:

  • (Hash)

Since:

  • 1.2.0



67
68
69
# File 'lib/couchbase/view_row.rb', line 67

def data
  @data
end

#doc ⇒ Hash

The document hash.

It usually available when view executed with :include_doc argument.

Returns:

  • (Hash)

Since:

  • 1.2.0



97
98
99
# File 'lib/couchbase/view_row.rb', line 97

def doc
  @doc
end

#id ⇒ String

The identificator of the document

Returns:

  • (String)

Since:

  • 1.2.0



104
105
106
# File 'lib/couchbase/view_row.rb', line 104

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.



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

def key
  @key
end

#meta ⇒ Hash

The meta data linked to the document

Returns:

  • (Hash)

Since:

  • 1.2.0



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

def meta
  @meta
end

#value ⇒ Object

The value which was emitted by map function



88
89
90
# File 'lib/couchbase/view_row.rb', line 88

def value
  @value
end

Class Method Details

.wrap(bucket, data) ⇒ ViewRow

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



150
151
152
# File 'lib/couchbase/view_row.rb', line 150

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

Parameters:

  • key (String) —

    the attribute name

Returns:

  • (Object) —

    property value or nil

Since:

  • 1.2.0



163
164
165
# File 'lib/couchbase/view_row.rb', line 163

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

#[]=(key, value) ⇒ Object

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



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

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

#has_key?(key) ⇒ true, false

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



175
176
177
# File 'lib/couchbase/view_row.rb', line 175

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

#inspect ⇒ Object

Since:

  • 1.2.0



202
203
204
205
206
207
208
209
# File 'lib/couchbase/view_row.rb', line 202

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

Returns:

  • (true, false) —

    true if this row is last in a stream

Since:

  • 1.2.1



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

def last?
  @last
end