module TinyDyno::HashKey::ClassMethods
Public Instance Methods
hash_key(name, options = {})
click to toggle source
Defines the primary key for the Document
Only one primary key = hash_key
is allowed in DynamoDB
@example Define a field.
hash_key :score, :type => Integer
@param [ Symbol ] name The name of the hash_key. @param [ Hash ] options The options to pass to the hash_key.
@option options [ Class ] :type The type of the field. @option options [ String ] :label The label for the field.
@return [ Field ] The generated field
# 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
Private Instance Methods
attribute_definition_meets_spec?(definition)
click to toggle source
# File lib/tiny_dyno/hash_key.rb, line 75 def attribute_definition_meets_spec?(definition) return (definition.has_key?(:attribute_name) && \ definition.has_key?(:attribute_type) && \ definition[:attribute_name].class == String && \ definition[:attribute_type].class == String && \ ['S','N', 'B'].include?(definition[:attribute_type])) end
attribute_definitions_meet_spec?()
click to toggle source
Return true or false, depending on whether the attribute_definitions on the model meet the specification of the Aws Sdk This is a syntax, not a logic check
# File lib/tiny_dyno/hash_key.rb, line 69 def attribute_definitions_meet_spec? attribute_definitions.each do |definition| return false unless attribute_definition_meets_spec?(definition) end end
build_attribute_definition(name, key_type)
click to toggle source
# File lib/tiny_dyno/hash_key.rb, line 83 def build_attribute_definition(name, key_type) { attribute_name: name, attribute_type: determine_key_class(key_type) } end
determine_key_class(key_type = nil)
click to toggle source
# File lib/tiny_dyno/hash_key.rb, line 90 def determine_key_class(key_type = nil) return 'S' if key_type == String return 'N' if key_type == Fixnum or key_type == Integer return nil end
dyno_typed_key(key:, val:)
click to toggle source
convert values in queries to DynamoDB into types as expected by DynamoDB
# File lib/tiny_dyno/hash_key.rb, line 105 def dyno_typed_key(key:, val:) field_type = self.fields[key].options[:type] return (TinyDyno::Adapter.aws_attribute(field_type: field_type, value: val)) end
hash_key_schema(name)
click to toggle source
# File lib/tiny_dyno/hash_key.rb, line 96 def hash_key_schema(name) { attribute_name: name, key_type: 'HASH' } end