Class: Couchbase::DesignDoc

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

Overview

This class encapsulates information about design docs

It is subclass of ViewRow, but also gives access to view creation through method_missing

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

Attributes inherited from ViewRow

#data, #doc, #id, #key, #meta, #value

Instance Method Summary collapse

Methods inherited from ViewRow

#[], #[]=, #has_key?, #last?, wrap

Constructor Details

#initialize(bucket, data) ⇒ DesignDoc

Initialize the design doc 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.1



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

def initialize(bucket, data)
  super
  @all_views = {}
  @views = @doc.has_key?('views') ? @doc['views'].keys : []
  @spatial = @doc.has_key?('spatial') ? @doc['spatial'].keys : []
  @views.each{|name| @all_views[name] = "#{@id}/_view/#{name}"}
  @spatial.each{|name| @all_views[name] = "#{@id}/_spatial/#{name}"}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Since:

  • 1.2.1



213
214
215
216
217
218
219
# File 'lib/couchbase/view_row.rb', line 213

def method_missing(meth, *args)
  if path = @all_views[meth.to_s]
    View.new(@bucket, path, *args)
  else
    super
  end
end

Instance Attribute Details

#spatialArray<View>

The list of spatial views defined or empty array

Returns:

Since:

  • 1.2.1



249
250
251
# File 'lib/couchbase/view_row.rb', line 249

def spatial
  @spatial
end

#viewsArray<View>

The list of views defined or empty array

Returns:

Since:

  • 1.2.1



242
243
244
# File 'lib/couchbase/view_row.rb', line 242

def views
  @views
end

Instance Method Details

#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.1



258
259
260
# File 'lib/couchbase/view_row.rb', line 258

def has_views?
  !@views.empty?
end

#inspectObject

Since:

  • 1.2.1



262
263
264
265
266
267
268
269
# File 'lib/couchbase/view_row.rb', line 262

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

#method(meth, *args) ⇒ Object

Since:

  • 1.2.1



229
230
231
232
233
234
235
# File 'lib/couchbase/view_row.rb', line 229

def method(meth, *args)
  if path = @all_views[meth.to_s]
    lambda{|*p| View.new(@bucket, path, *p)}
  else
    super
  end
end

#respond_to?(meth, *args) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 1.2.1



221
222
223
224
225
226
227
# File 'lib/couchbase/view_row.rb', line 221

def respond_to?(meth, *args)
  if @all_views[meth.to_s]
    true
  else
    super
  end
end