Module: Vident::Typed::Attributes

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

Instance Method Summary collapse

Instance Method Details

#attribute(key) ⇒ Object Also known as: []



23
24
25
26
27
28
# File 'lib/vident/typed/attributes.rb', line 23

def attribute(key)
  if Rails.env.development? && !key?(key)
    raise StandardError, "Attribute #{key} not found in #{self.class.name}"
  end
  @__attributes.attributes[key]
end

#attribute_namesObject



19
20
21
# File 'lib/vident/typed/attributes.rb', line 19

def attribute_names
  @attribute_names ||= self.class.attribute_names
end

#attributesObject



15
16
17
# File 'lib/vident/typed/attributes.rb', line 15

def attributes
  @__attributes.attributes
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/vident/typed/attributes.rb', line 31

def key?(key)
  self.class.schema.attribute_names.include?(key)
end

#prepare_attributes(attributes) ⇒ Object

TODO: better handling of when either class.schema is undefined (as no attributes configured) or when other methods ar called before prepare_attributes is called



11
12
13
# File 'lib/vident/typed/attributes.rb', line 11

def prepare_attributes(attributes)
  @__attributes = self.class.schema.new(**attributes)
end

#to_hashObject



35
36
37
# File 'lib/vident/typed/attributes.rb', line 35

def to_hash
  @__attributes.to_h
end