Module: Vident::Attributes::NotTyped
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/vident/attributes/not_typed.rb
Instance Method Summary collapse
- #attribute(key) ⇒ Object
- #attribute_names ⇒ Object
- #attributes ⇒ Object
- #prepare_attributes(attributes) ⇒ Object
- #to_hash ⇒ Object
Instance Method Details
#attribute(key) ⇒ Object
34 35 36 |
# File 'lib/vident/attributes/not_typed.rb', line 34 def attribute(key) attributes[key] end |
#attribute_names ⇒ Object
30 31 32 |
# File 'lib/vident/attributes/not_typed.rb', line 30 def attribute_names self.class.attribute_names end |
#attributes ⇒ Object
8 9 10 |
# File 'lib/vident/attributes/not_typed.rb', line 8 def attributes @__attributes ||= {} end |
#prepare_attributes(attributes) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/vident/attributes/not_typed.rb', line 12 def prepare_attributes(attributes) @__attributes ||= {} attribute_names.each do |attr_name| = self.class. default = &.dig(attr_name, :default) allow_nil = [attr_name] ? [attr_name].fetch(:allow_nil, true) : true if attributes&.include? attr_name value = attributes[attr_name] @__attributes[attr_name] = (value.nil? && default) ? default : value else @__attributes[attr_name] = default end raise ArgumentError, "Attribute #{attr_name} cannot be nil" if @__attributes[attr_name].nil? && !allow_nil instance_variable_set(self.class.attribute_ivar_names[attr_name], @__attributes[attr_name]) end end |
#to_hash ⇒ Object
38 39 40 |
# File 'lib/vident/attributes/not_typed.rb', line 38 def to_hash attributes.dup end |