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



214
215
216
# File 'lib/vident/attributes/typed.rb', line 214

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

Instance Method Details

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



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

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



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

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

#attributesObject



22
23
24
# File 'lib/vident/attributes/typed.rb', line 22

def attributes
  @__attributes.attributes
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#to_hashObject



42
43
44
# File 'lib/vident/attributes/typed.rb', line 42

def to_hash
  @__attributes.to_h
end