Class: Couchbase::ViewRow

Inherits:
Object
  • Object
show all
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

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. It will define view methods if the data object looks like design document.

Parameters:

  • bucket (Couchbase::Bucket)

    the reference to connection

  • data (Hash)

    the data hash, which was built from JSON document representation

Since:

  • 1.2.0



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

#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:

  • (String)

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

#spatialArray<View>

The list of spatial views defined or empty array

Returns:

Since:

  • 1.2.0



99
100
101
# File 'lib/couchbase/view_row.rb', line 99

def spatial
  @spatial
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

#viewsArray<View>

The list of views defined or empty array

Returns:

Since:

  • 1.2.0



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

def views
  @views
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



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

def self.wrap(bucket, data)
  ViewRow.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



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

Parameters:

  • key (String)

    the attribute name

  • value (Object)

    the attribute value

Returns:

  • (Object)

    the value

Since:

  • 1.2.0



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

Returns:

  • (true, false)

Since:

  • 1.2.0



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

Parameters:

  • key (String)

    the attribute name

Returns:

  • (true, false)

    true if the given attribute is present in in the document.

Since:

  • 1.2.0



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

Returns:

  • (true, false)

    true if the document have views

See Also:

Since:

  • 1.2.0



217
218
219
# File 'lib/couchbase/view_row.rb', line 217

def has_views?
  !!(design_doc? && !@views.empty?)
end

#inspectObject

Since:

  • 1.2.0



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