Module: TinyDyno::HashKey::ClassMethods

Defined in:
lib/tiny_dyno/hash_key.rb

Instance Method Summary collapse

Instance Method Details

#hash_key(name, options = {}) ⇒ Field

Defines the primary key for the Document Only one primary key = hash_key is allowed in DynamoDB

Examples:

Define a field.

hash_key :score, :type => Integer

Parameters:

  • name (Symbol)

    The name of the hash_key.

  • options (Hash) (defaults to: {})

    The options to pass to the hash_key.

Options Hash (options):

  • :type (Class)

    The type of the field.

  • :label (String)

    The label for the field.

Returns:

  • (Field)

    The generated field

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tiny_dyno/hash_key.rb', line 45

def hash_key(name, options = {})
  raise TinyDyno::Errors::OnlyOneHashKeyPermitted.new(klass: self.class, name: name) unless primary_key.empty?
  named = name.to_s
  attribute_definition = build_attribute_definition(named,options[:type])
  key_schema = hash_key_schema(named)
  unless attribute_definition_meets_spec?(attribute_definition)
    raise TinyDyno::Errors::InvalidHashKey.new(klass: self.class, name: name)
  end
  # we need the accessors as well
  add_field(named, options)
  self.attribute_definitions << attribute_definition
  self.key_schema << key_schema
  self.primary_key = {
      attribute_name: attribute_definition[:attribute_name],
      attribute_type: attribute_definition[:attribute_type],
      key_type: key_schema[:key_type],
  }
end