module Mara::Model::Query::ClassMethods

Helper methods defined on the class.

@author Maddie Schipper @since 1.0.0

Public Instance Methods

_find(partition_key, sort_key = nil) click to toggle source

@private

@see find

# 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) click to toggle source

Find a single object with the matching partition_key and sort key.

@param partition_key [#to_s] The value for the partition key.

@param sort_key [#to_s, nil] The value for the sort key.

@raise [NotFoundError] If the object doesn't exist in the table for

the requested primary key.

@raise [ArgumentError] If the partition_key is blank, or the

+sort_key+ is blank and the class defines a sort_key name.

@return [ Mara::Model::Base]

# 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