module Dinamo::Model::Attributes::ClassMethods

Public Instance Methods

attribute_method?(attr) click to toggle source
# File lib/dinamo/model/attributes.rb, line 42
def attribute_method?(attr)
  attribute_methods.include?(attr)
end
attribute_method_already_implemented?(attr) click to toggle source
# File lib/dinamo/model/attributes.rb, line 46
def attribute_method_already_implemented?(attr)
  attribute_method?(attr) && respond_to?(attr) && respond_to?("#{attr}=")
end
attribute_methods() click to toggle source
# File lib/dinamo/model/attributes.rb, line 38
def attribute_methods
  @attribute_names ||= []
end
cast(type, &block) click to toggle source
# File lib/dinamo/model/attributes.rb, line 109
def cast(type, &block)
  caster.register(type, &block)
end
caster() click to toggle source
# File lib/dinamo/model/attributes.rb, line 105
def caster
  @caster ||= Caster.new
end
default_values() click to toggle source
# File lib/dinamo/model/attributes.rb, line 65
def default_values
  Hash[supported_fields.select { |field| not field.default.nil? }
    .map { |field| [field.name, field.default] }].with_indifferent_access
end
define_attribute_method(attr) click to toggle source
# File lib/dinamo/model/attributes.rb, line 54
def define_attribute_method(attr)
  return if attribute_method_already_implemented?(attr)
  define_method(attr) { @attributes[attr] }
  define_method("#{attr}=") do |val|
    with_callback :attribute_update, attr, val do
      @attributes[attr] = val
    end
  end
  attribute_methods << attr
end
define_attribute_methods(*attrs) click to toggle source
# File lib/dinamo/model/attributes.rb, line 50
def define_attribute_methods(*attrs)
  attrs.each { |attr| define_attribute_method(attr) }
end
field(key, type: nil, **options) click to toggle source
# File lib/dinamo/model/attributes.rb, line 91
def field(key, type: nil, **options)
  caster.associate(key, type) if type
  supported_fields << Key.new(key.to_s, type: type, **options)
  define_attribute_method(key) unless respond_to?(:"#{key}=")
end
hash_key(key, **options) click to toggle source
# File lib/dinamo/model/attributes.rb, line 97
def hash_key(key, **options)
  primary_key :hash, key, **options
end
primary_key(kind, key, type: nil, **options) click to toggle source
# File lib/dinamo/model/attributes.rb, line 74
def primary_key(kind, key, type: nil, **options)
  name = :"@#{kind}_key"
  var = instance_variable_get(name)
  primary_keys[kind] = key
  var ? var : instance_variable_set(name, key.to_s)
  define_attribute_method key
  supported_fields << Key.new(key.to_s, type: type, required: true, primary: true)
end
primary_keys() click to toggle source
# File lib/dinamo/model/attributes.rb, line 70
def primary_keys
  @primary_keys ||= {}
end
range_key(key, **options) click to toggle source
# File lib/dinamo/model/attributes.rb, line 101
def range_key(key, **options)
  primary_key :range, key, **options
end
required_fields() click to toggle source
# File lib/dinamo/model/attributes.rb, line 87
def required_fields
  supported_fields.select(&:required?)
end
supported_fields() click to toggle source
# File lib/dinamo/model/attributes.rb, line 83
def supported_fields
  @supported_fields ||= []
end