Module: Vident::Attributes::NotTyped

Extended by:
ActiveSupport::Concern
Defined in:
lib/vident/attributes/not_typed.rb

Instance Method Summary collapse

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_namesObject



30
31
32
# File 'lib/vident/attributes/not_typed.rb', line 30

def attribute_names
  self.class.attribute_names
end

#attributesObject



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|
    options = self.class.attribute_options
    default = options&.dig(attr_name, :default)
    allow_nil = options[attr_name] ? options[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_hashObject



38
39
40
# File 'lib/vident/attributes/not_typed.rb', line 38

def to_hash
  attributes.dup
end