Module: Vident::Attributes::Typed

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



174
175
176
# File 'lib/vident/attributes/typed.rb', line 174

def self.included(base)
  raise "Vident::Attributes::Typed requires dry-struct to be installed"
end

Instance Method Details

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



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

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



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

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

#attributesObject



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

def attributes
  @__attributes.attributes
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#prepare_attributes(attributes) ⇒ Object



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

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

#to_hashObject



40
41
42
# File 'lib/vident/attributes/typed.rb', line 40

def to_hash
  @__attributes.to_h
end