Module: Dynamoid::Finders::ClassMethods
- Defined in:
- lib/dynamoid/finders.rb
Instance Method Summary collapse
-
#find(*ids) ⇒ Dynamoid::Document
Find one or many objects, specified by one id or an array of ids.
-
#find_all(ids, options = {}) ⇒ Object
Return objects found by the given array of ids, either hash keys, or hash/range key combinations using BatchGet.
-
#find_all_by_composite_key(hash_key, options = {}) ⇒ Array
Find all objects by hash and range keys.
-
#find_by_composite_key(hash_key, range_key, options = {}) ⇒ Object
Find one object directly by hash and range keys.
-
#find_by_id(id, options = {}) ⇒ Dynamoid::Document
Find one object directly by id.
-
#method_missing(method, *args) ⇒ Dynamoid::Document/Array
Find using exciting method_missing finders attributes.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Dynamoid::Document/Array
Find using exciting method_missing finders attributes. Uses criteria chains under the hood to accomplish this neatness.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/dynamoid/finders.rb', line 114 def method_missing(method, *args) if method =~ /find/ finder = method.to_s.split('_by_').first attributes = method.to_s.split('_by_').last.split('_and_') chain = Dynamoid::Criteria::Chain.new(self) chain.query = Hash.new.tap {|h| attributes.each_with_index {|attr, index| h[attr.to_sym] = args[index]}} if finder =~ /all/ return chain.all else return chain.first end else super end end |
Instance Method Details
#find(*ids) ⇒ Dynamoid::Document
Find one or many objects, specified by one id or an array of ids.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dynamoid/finders.rb', line 18 def find(*ids) = if ids.last.is_a? Hash ids.slice!(-1) else {} end expects_array = ids.first.kind_of?(Array) ids = Array(ids.flatten.uniq) if ids.count == 1 result = self.find_by_id(ids.first, ) expects_array ? Array(result) : result else find_all(ids) end end |
#find_all(ids, options = {}) ⇒ Object
Return objects found by the given array of ids, either hash keys, or hash/range key combinations using BatchGet. Returns empty array if no results found.
47 48 49 50 |
# File 'lib/dynamoid/finders.rb', line 47 def find_all(ids, = {}) items = Dynamoid.adapter.read(self.table_name, ids, ) items ? items[self.table_name].map{|i| from_database(i)} : [] end |
#find_all_by_composite_key(hash_key, options = {}) ⇒ Array
Find all objects by hash and range keys.
97 98 99 100 101 |
# File 'lib/dynamoid/finders.rb', line 97 def find_all_by_composite_key(hash_key, = {}) Dynamoid.adapter.query(self.table_name, .merge({hash_value: hash_key})).collect do |item| from_database(item) end end |
#find_by_composite_key(hash_key, range_key, options = {}) ⇒ Object
Find one object directly by hash and range keys
72 73 74 |
# File 'lib/dynamoid/finders.rb', line 72 def find_by_composite_key(hash_key, range_key, = {}) find_by_id(hash_key, .merge({:range_key => range_key})) end |
#find_by_id(id, options = {}) ⇒ Dynamoid::Document
Find one object directly by id.
59 60 61 62 63 64 65 |
# File 'lib/dynamoid/finders.rb', line 59 def find_by_id(id, = {}) if item = Dynamoid.adapter.read(self.table_name, id, ) from_database(item) else nil end end |