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


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/couchbase/view_row.rb', line 96

def initialize(bucket, data)
  @bucket = bucket
  @data = data
  @key = data[S_KEY]
  @value = data[S_VALUE]
  if data[S_DOC]
    @meta = data[S_DOC][S_META]
    @doc = data[S_DOC][S_VALUE]
  end
  @id = data[S_ID] || @meta && @meta[S_ID]
  @last = data.delete(S_IS_LAST) || false
end

Instance Attribute Details

#dataHash

The hash built from JSON document.

This is complete response from the Couchbase

Returns:

  • (Hash)

Since:

  • 1.2.0


41
42
43
# File 'lib/couchbase/view_row.rb', line 41

def data
  @data
end

#docHash

The document hash.

It usually available when view executed with :include_doc argument.

Returns:

  • (Hash)

Since:

  • 1.2.0


71
72
73
# File 'lib/couchbase/view_row.rb', line 71

def doc
  @doc
end

#idString

The identificator of the document

Returns:

Since:

  • 1.2.0


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

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.


53
54
55
# File 'lib/couchbase/view_row.rb', line 53

def key
  @key
end

#metaHash

The meta data linked to the document

Returns:

  • (Hash)

Since:

  • 1.2.0


85
86
87
# File 'lib/couchbase/view_row.rb', line 85

def meta
  @meta
end

#valueObject

The value which was emitted by map function


62
63
64
# File 'lib/couchbase/view_row.rb', line 62

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


120
121
122
# File 'lib/couchbase/view_row.rb', line 120

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


133
134
135
# File 'lib/couchbase/view_row.rb', line 133

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


159
160
161
# File 'lib/couchbase/view_row.rb', line 159

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


145
146
147
# File 'lib/couchbase/view_row.rb', line 145

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

#inspectObject

Since:

  • 1.2.0


172
173
174
175
176
177
178
179
# File 'lib/couchbase/view_row.rb', line 172

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


168
169
170
# File 'lib/couchbase/view_row.rb', line 168

def last?
  @last
end