Module: Ripple::Document::Finders::ClassMethods

Defined in:
lib/ripple/document/finders.rb

Instance Method Summary collapse

Instance Method Details

#find(key) ⇒ Document #find(key1, key2, ...) ⇒ Array<Document> #find(keylist) ⇒ Array<Document>

Retrieve single or multiple documents from Riak.

Overloads:

  • #find(key) ⇒ Document

    Find a single document.

    Parameters:

    • key (String)

      the key of a document to find

    Returns:

    • (Document)

      the found document, or nil

  • #find(key1, key2, ...) ⇒ Array<Document>

    Find a list of documents.

    Parameters:

    • key1 (String)

      the key of a document to find

    • key2 (String)

      the key of a document to find

    Returns:

    • (Array<Document>)

      a list of found documents, including nil for missing documents

  • #find(keylist) ⇒ Array<Document>

    Find a list of documents.

    Parameters:

    • keylist (Array<String>)

      an array of keys to find

    Returns:

    • (Array<Document>)

      a list of found documents, including nil for missing documents



49
50
51
52
53
54
55
56
57
58
# File 'lib/ripple/document/finders.rb', line 49

def find(*args)
  if args.first.is_a?(Array)
    args.flatten.map {|key| find_one(key) }
  else
    args.flatten!
    return nil if args.empty? || args.all?(&:blank?)
    return find_one(args.first) if args.size == 1
    args.map {|key| find_one(key) }
  end
end

#find!(*args) ⇒ Object

Retrieve single or multiple documents from Riak but raise Ripple::DocumentNotFound if a key can not be found in the bucket.

Raises:



63
64
65
66
67
# File 'lib/ripple/document/finders.rb', line 63

def find!(*args)
  found = find(*args)
  raise DocumentNotFound.new(args, found) if !found || Array(found).include?(nil)
  found
end

#firstObject

Find the first object using the first key in the bucket’s keys using find. You should not expect to actually get the first object you added to the bucket. This is just a convenience method.



73
74
75
# File 'lib/ripple/document/finders.rb', line 73

def first
  find(bucket.keys.first)
end

#first!Object

Find the first object using the first key in the bucket’s keys using find!



79
80
81
# File 'lib/ripple/document/finders.rb', line 79

def first!
  find!(bucket.keys.first)
end

#listArray<Document> #list {|Document| ... } ⇒ Object

Note:

This operation is incredibly expensive and should not be used in production applications.

Find all documents in the Document’s bucket and return them.

Overloads:

  • #listArray<Document>

    Get all documents and return them in an array.

    Parameters:

    • options (Hash)

      options to be passed to the underlying Bucket#keys method.

    Returns:

    • (Array<Document>)

      all found documents in the bucket

  • #list {|Document| ... } ⇒ Object

    Stream all documents in the bucket through the block.

    Yields:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ripple/document/finders.rb', line 94

def list
  if block_given?
    bucket.keys do |keys|
      keys.each do |key|
        obj = find_one(key)
        yield obj if obj
      end
    end
    []
  else
    bucket.keys.inject([]) do |acc, k|
      obj = find_one(k)
      obj ? acc << obj : acc
    end
  end
end