Module: Dinamo::Model::Attributes::ClassMethods

Defined in:
lib/dinamo/model/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute_method?(attr) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/dinamo/model/attributes.rb', line 32

def attribute_method?(attr)
  attribute_methods.include?(attr)
end

#attribute_method_already_implemented?(attr) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/dinamo/model/attributes.rb', line 36

def attribute_method_already_implemented?(attr)
  attribute_method?(attr) && respond_to?(attr) && respond_to?("#{attr}=")
end

#attribute_methodsObject



28
29
30
# File 'lib/dinamo/model/attributes.rb', line 28

def attribute_methods
  @attribute_names ||= []
end

#cast(type, &block) ⇒ Object



94
95
96
# File 'lib/dinamo/model/attributes.rb', line 94

def cast(type, &block)
  caster.register(type, &block)
end

#casterObject



90
91
92
# File 'lib/dinamo/model/attributes.rb', line 90

def caster
  @caster ||= Caster.new
end

#define_attribute_method(attr) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/dinamo/model/attributes.rb', line 44

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) ⇒ Object



40
41
42
# File 'lib/dinamo/model/attributes.rb', line 40

def define_attribute_methods(*attrs)
  attrs.each { |attr| define_attribute_method(attr) }
end

#field(key, type: nil, **options) ⇒ Object



76
77
78
79
80
# File 'lib/dinamo/model/attributes.rb', line 76

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) ⇒ Object



82
83
84
# File 'lib/dinamo/model/attributes.rb', line 82

def hash_key(key, **options)
  primary_key :hash, key, **options
end

#primary_key(kind, key, type: nil, **options) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/dinamo/model/attributes.rb', line 59

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_keysObject



55
56
57
# File 'lib/dinamo/model/attributes.rb', line 55

def primary_keys
  @primary_keys ||= {}
end

#range_key(key, **options) ⇒ Object



86
87
88
# File 'lib/dinamo/model/attributes.rb', line 86

def range_key(key, **options)
  primary_key :range, key, **options
end

#required_fieldsObject



72
73
74
# File 'lib/dinamo/model/attributes.rb', line 72

def required_fields
  supported_fields.select(&:required?)
end

#supported_fieldsObject



68
69
70
# File 'lib/dinamo/model/attributes.rb', line 68

def supported_fields
  @supported_fields ||= []
end