Module: Dinamo::Model::Attributes::ClassMethods
- Defined in:
- lib/dinamo/model/attributes.rb
Instance Method Summary collapse
- #attribute_method?(attr) ⇒ Boolean
- #attribute_method_already_implemented?(attr) ⇒ Boolean
- #attribute_methods ⇒ Object
- #cast(type, &block) ⇒ Object
- #caster ⇒ Object
- #default_values ⇒ Object
- #define_attribute_method(attr) ⇒ Object
- #define_attribute_methods(*attrs) ⇒ Object
- #field(key, type: nil, **options) ⇒ Object
- #hash_key(key, **options) ⇒ Object
- #primary_key(kind, key, type: nil, **options) ⇒ Object
- #primary_keys ⇒ Object
- #range_key(key, **options) ⇒ Object
- #required_fields ⇒ Object
- #supported_fields ⇒ Object
Instance Method Details
#attribute_method?(attr) ⇒ Boolean
42 43 44 |
# File 'lib/dinamo/model/attributes.rb', line 42 def attribute_method?(attr) attribute_methods.include?(attr) end |
#attribute_method_already_implemented?(attr) ⇒ Boolean
46 47 48 |
# 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 ⇒ Object
38 39 40 |
# File 'lib/dinamo/model/attributes.rb', line 38 def attribute_methods @attribute_names ||= [] end |
#cast(type, &block) ⇒ Object
109 110 111 |
# File 'lib/dinamo/model/attributes.rb', line 109 def cast(type, &block) caster.register(type, &block) end |
#caster ⇒ Object
105 106 107 |
# File 'lib/dinamo/model/attributes.rb', line 105 def caster @caster ||= Caster.new end |
#default_values ⇒ Object
65 66 67 68 |
# 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) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# 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) ⇒ Object
50 51 52 |
# 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) ⇒ Object
91 92 93 94 95 |
# File 'lib/dinamo/model/attributes.rb', line 91 def field(key, type: nil, **) caster.associate(key, type) if type supported_fields << Key.new(key.to_s, type: type, **) define_attribute_method(key) unless respond_to?(:"#{key}=") end |
#hash_key(key, **options) ⇒ Object
97 98 99 |
# File 'lib/dinamo/model/attributes.rb', line 97 def hash_key(key, **) primary_key :hash, key, ** end |
#primary_key(kind, key, type: nil, **options) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/dinamo/model/attributes.rb', line 74 def primary_key(kind, key, type: nil, **) 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 ⇒ Object
70 71 72 |
# File 'lib/dinamo/model/attributes.rb', line 70 def primary_keys @primary_keys ||= {} end |
#range_key(key, **options) ⇒ Object
101 102 103 |
# File 'lib/dinamo/model/attributes.rb', line 101 def range_key(key, **) primary_key :range, key, ** end |
#required_fields ⇒ Object
87 88 89 |
# File 'lib/dinamo/model/attributes.rb', line 87 def required_fields supported_fields.select(&:required?) end |
#supported_fields ⇒ Object
83 84 85 |
# File 'lib/dinamo/model/attributes.rb', line 83 def supported_fields @supported_fields ||= [] end |