Module: Valuables::Entity::ClassMethods

Defined in:
lib/valuables/entity.rb

Instance Method Summary collapse

Instance Method Details

#attributes(*names) ⇒ Object

Define an explicit list of attribute names.

When you explicitly define attributes, you ensure objects can only be created with those attributes. Missing or unexpected attributes will trigger an ArgumentError.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/valuables/entity.rb', line 18

def attributes(*names)
  return @attributes unless names.any?
  @attributes = names
  mod = Module.new do
    names.each do |name|
      define_method(name) do
        @attributes.fetch(name)
      end
    end
  end
  include mod
end