Module: Ork::Model::Finders
- Defined in:
- lib/ork/model/finders.rb
Instance Method Summary collapse
-
#[](id) ⇒ Object
Retrieve a record by ID.
-
#all(keys = nil) ⇒ Object
(also: #list)
Find all documents of specified keys and return them.
-
#exist?(id) ⇒ Boolean
(also: #exists?)
Check if the ID exists.
-
#find(by_index, value, options = {}) ⇒ Object
Find values in indexed fields.
Instance Method Details
#[](id) ⇒ Object
Retrieve a record by ID.
Example:
u = User.create
u == User[u.id]
# => true
14 15 16 |
# File 'lib/ork/model/finders.rb', line 14 def [](id) load_key(id) if exist?(id) end |
#all(keys = nil) ⇒ Object Also known as: list
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.
32 33 34 |
# File 'lib/ork/model/finders.rb', line 32 def all(keys = nil) Ork::ResultSet.all(self, keys) end |
#exist?(id) ⇒ Boolean Also known as: exists?
Check if the ID exists.
19 20 21 |
# File 'lib/ork/model/finders.rb', line 19 def exist?(id) !id.nil? && bucket.exists?(id) end |
#find(by_index, value, options = {}) ⇒ Object
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.
63 64 65 66 67 68 |
# File 'lib/ork/model/finders.rb', line 63 def find(by_index, value, = {}) raise Ork::IndexNotFound unless indices.has_key? by_index index = indices[by_index] Ork::ResultSet.new(self, index, value, ) end |