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_all_by_secondary_index(hash, options = {}) ⇒ Array
Find all objects by using local secondary or global secondary index.
-
#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.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/dynamoid/finders.rb', line 189 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.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dynamoid/finders.rb', line 28 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, ) if result.nil? = "Couldn't find #{self.name} with '#{self.hash_key}'=#{ids[0]}" raise Errors::RecordNotFound.new() end expects_array ? Array(result) : result else result = find_all(ids) if result.size != ids.size = "Couldn't find all #{self.name.pluralize} with '#{self.hash_key}': (#{ids.join(', ')}) " << "(found #{result.size} results, but was looking for #{ids.size})" raise Errors::RecordNotFound.new() end result 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.
67 68 69 70 |
# File 'lib/dynamoid/finders.rb', line 67 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.
117 118 119 120 121 |
# File 'lib/dynamoid/finders.rb', line 117 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_all_by_secondary_index(hash, options = {}) ⇒ Array
Find all objects by using local secondary or global secondary index
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/dynamoid/finders.rb', line 143 def find_all_by_secondary_index(hash, = {}) range = [:range] || {} hash_key_field, hash_key_value = hash.first range_key_field, range_key_value = range.first range_op_mapped = nil if range_key_field range_key_field = range_key_field.to_s range_key_op = 'eq' if range_key_field.include?('.') range_key_field, range_key_op = range_key_field.split('.', 2) end range_op_mapped = RANGE_MAP.fetch(range_key_op) end # Find the index index = self.find_index(hash_key_field, range_key_field) raise Dynamoid::Errors::MissingIndex.new("attempted to find #{[hash_key_field, range_key_field]}") if index.nil? # query opts = { hash_key: hash_key_field.to_s, hash_value: hash_key_value, index_name: index.name, } if range_key_field opts[:range_key] = range_key_field opts[range_op_mapped] = range_key_value end = opts.merge(.reject {|key, _| key == :range }) Dynamoid.adapter.query(self.table_name, ).map 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
92 93 94 |
# File 'lib/dynamoid/finders.rb', line 92 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.
79 80 81 82 83 84 85 |
# File 'lib/dynamoid/finders.rb', line 79 def find_by_id(id, = {}) if item = Dynamoid.adapter.read(self.table_name, id, ) from_database(item) else nil end end |