Class: CouchDB::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/couchdb/collection.rb

Overview

Collection is a proxy class for the result-set of a CouchDB view. It provides all read-only methods of an array. The loading of content is lazy and will be triggered on the first request.

Defined Under Namespace

Classes: DocumentsProxy

Constant Summary collapse

REQUEST_PARAMETER_KEYS =
[
  :key, :startkey, :startkey_docid, :endkey, :endkey_docid,
  :limit, :stale, :descending, :skip, :group, :group_level,
  :reduce, :inclusive_end, :include_docs
].freeze
ARRAY_METHOD_NAMES =
[
  :[], :at, :collect, :compact, :count, :cycle, :each, :each_index,
  :empty?, :fetch, :index, :first, :flatten, :include?, :join, :last,
  :length, :map, :pack, :reject, :reverse, :reverse_each, :rindex,
  :sample, :shuffle, :size, :slice, :sort, :take, :to_a, :to_ary,
  :values_at, :zip
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, url, options = { }) ⇒ Collection

Returns a new instance of Collection.



28
29
30
31
# File 'lib/couchdb/collection.rb', line 28

def initialize(database, url, options = { })
  @database, @url, @options = database, url, options
  @documents = DocumentsProxy.new self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/couchdb/collection.rb', line 42

def method_missing(method_name, *arguments, &block)
  if ARRAY_METHOD_NAMES.include?(method_name)
    fetch
    @entries.send method_name, *arguments, &block
  else
    super
  end
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



23
24
25
# File 'lib/couchdb/collection.rb', line 23

def database
  @database
end

#documentsObject (readonly)

Returns the value of attribute documents.



26
27
28
# File 'lib/couchdb/collection.rb', line 26

def documents
  @documents
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/couchdb/collection.rb', line 25

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



24
25
26
# File 'lib/couchdb/collection.rb', line 24

def url
  @url
end

Instance Method Details

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/couchdb/collection.rb', line 38

def respond_to?(method_name)
  ARRAY_METHOD_NAMES.include?(method_name) || super
end

#total_countObject



33
34
35
36
# File 'lib/couchdb/collection.rb', line 33

def total_count
  fetch_meta unless @total_count
  @total_count
end