Module: Mara::Model::Query::ClassMethods

Defined in:
lib/mara/model/query.rb

Overview

Helper methods defined on the class.

Author:

  • Maddie Schipper

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#_find(partition_key, sort_key = nil) ⇒ Object

See Also:

Since:

  • 1.0.0



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.

Parameters:

  • partition_key (#to_s)

    The value for the partition key.

  • sort_key (#to_s, nil) (defaults to: nil)

    The value for the sort key.

Returns:

Raises:

  • (NotFoundError)

    If the object doesn’t exist in the table for the requested primary key.

  • (ArgumentError)

    If the partition_key is blank, or the sort_key is blank and the class defines a sort_key name.

Since:

  • 1.0.0



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