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

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

Instance Method Summary (collapse)

Instance Method Details

- (Array<Document>) all - (Object) all {|Document| ... }

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

Overloads:

  • - (Array<Document>) all

    Get all documents and return them in an array.

    Parameters:

    • options (Hash)

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

    Returns:

  • - (Object) all {|Document| ... }

    Stream all documents in the bucket through the block.

    Yields:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'ripple/lib/ripple/document/finders.rb', line 101

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

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

Retrieve single or multiple documents from Riak.

Overloads:

  • - (Document) find(key)

    Find a single document.

    Parameters:

    • key (String)

      the key of a document to find

    Returns:

    • (Document)

      the found document, or nil

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

    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

  • - (Array<Document>) find(keylist)

    Find a list of documents.

    Parameters:

    Returns:

    • (Array<Document>)

      a list of found documents, including nil for missing documents



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

def find(*args)
  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

- (Object) find!(*args)

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

Raises:



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

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

- (Object) first

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.



82
83
84
# File 'ripple/lib/ripple/document/finders.rb', line 82

def first
  find(bucket.keys.first)
end

- (Object) first!

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



88
89
90
# File 'ripple/lib/ripple/document/finders.rb', line 88

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