Module: Dynamoid::Finders::ClassMethods
- Defined in:
- lib/dynamoid/finders.rb
Instance Method Summary (collapse)
-
- (Dynamoid::Document) find(*id)
Find one or many objects, specified by one id or an array of ids.
-
- (Dynamoid::Document) find_by_id(id)
Find one object directly by id.
-
- (Dynamoid::Document/Array) method_missing(method, *args)
Find using exciting method_missing finders attributes.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Dynamoid::Document/Array) method_missing(method, *args)
Find using exciting method_missing finders attributes. Uses criteria chains under the hood to accomplish this neatness.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dynamoid/finders.rb', line 56 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
- (Dynamoid::Document) find(*id)
Find one or many objects, specified by one id or an array of ids.
18 19 20 21 22 23 24 25 26 |
# File 'lib/dynamoid/finders.rb', line 18 def find(*id) id = Array(id.flatten.uniq) if id.count == 1 self.find_by_id(id.first) else items = Dynamoid::Adapter.read(self.table_name, id) items[self.table_name].collect{|i| self.build(i).tap { |o| o.new_record = false } } end end |
- (Dynamoid::Document) find_by_id(id)
Find one object directly by id.
35 36 37 38 39 40 41 42 43 |
# File 'lib/dynamoid/finders.rb', line 35 def find_by_id(id) if item = Dynamoid::Adapter.read(self.table_name, id) obj = self.new(item) obj.new_record = false return obj else return nil end end |