module Ork::Model::Finders

Public Instance Methods

[](id) click to toggle source

Retrieve a record by ID.

Example:

u = User.create
u == User[u.id]
# => true
# File lib/ork/model/finders.rb, line 14
def [](id)
  load_key(id) if exist?(id)
end
all(keys = nil) click to toggle source

Find all documents of specified keys and return them

When nil, find all documents in the Document’s bucket and return them.

@return Ork::ResultSet<Document> all found documents in the bucket

@Note: This operation can be incredibly expensive and should not

be used in production applications.
# File lib/ork/model/finders.rb, line 32
def all(keys = nil)
  Ork::ResultSet.all(self, keys)
end
Also aliased as: list
exist?(id) click to toggle source

Check if the ID exists.

# File lib/ork/model/finders.rb, line 19
def exist?(id)
  !id.nil? && bucket.exists?(id)
end
Also aliased as: exists?
exists?(id)
Alias for: exist?
find(by_index, value, options = {}) click to toggle source

Find values in indexed fields.

@return Ork::ResultSet<Document> found documents in the bucket

options - Hash configs for pagination.

:max_results  - Number
:continuation - String

Example:

class User
  include Ork::Document

  attribute :name
  index :name
end

u = User.create(name: 'John')
User.find(:name, 'John', max_results: 5).include?(u)
# => true

User.find(:name, 'Mike').include?(u)
# => false

Note: If the key was not defined, an ‘Ork::IndexNotFound` exception is raised.

# File lib/ork/model/finders.rb, line 63
def find(by_index, value, options = {})
  raise Ork::IndexNotFound unless indices.has_key? by_index

  index = indices[by_index]
  Ork::ResultSet.new(self, index, value, options)
end
list(keys = nil)
Alias for: all

Private Instance Methods

load_key(id) click to toggle source
# File lib/ork/model/finders.rb, line 72
def load_key(id)
  new.send(:load!, id)
rescue Riak::FailedRequest => e
  raise e unless e.not_found?
end