Module: Mara::Model::Query::ClassMethods
- Defined in:
- lib/mara/model/query.rb
Overview
Helper methods defined on the class.
Instance Method Summary collapse
- #_find(partition_key, sort_key = nil) ⇒ Object
-
#find(partition_key, sort_key = nil) ⇒ Mara::Model::Base
Find a single object with the matching partition_key and sort key.
Instance Method Details
#_find(partition_key, sort_key = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mara/model/query.rb', line 57 def _find(partition_key, sort_key = nil) if partition_key.blank? raise ArgumentError, 'Must specify a valid partition key value' end if sort_key.nil? && !self.sort_key.blank? raise ArgumentError, "#{self.class.name} specifies a sort key, but no sort key value was given." end key_params = {} key_params[self.partition_key] = Mara::AttributeFormatter.format(partition_key) if self.sort_key.present? key_params[self.sort_key] = Mara::AttributeFormatter.format(sort_key) end response = Mara::Query.get_item(key: key_params) if response.nil? || response.items.empty? raise NotFoundError, "Can't find item with pk=#{partition_key} sk=#{sort_key}" end item = response.items[0] construct(item) end |
#find(partition_key, sort_key = nil) ⇒ Mara::Model::Base
Find a single object with the matching partition_key and sort key.
47 48 49 50 51 |
# File 'lib/mara/model/query.rb', line 47 def find(partition_key, sort_key = nil) Mara.instrument('model.find', class_name: name, partition_key: partition_key, sort_key: sort_key) do _find(partition_key, sort_key) end end |