Class: Definition::ValueObject

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/definition/value_object.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil, **kwargs) ⇒ ValueObject

Returns a new instance of ValueObject.



17
18
19
20
21
22
# File 'lib/definition/value_object.rb', line 17

def initialize(args = nil, **kwargs)
  result = self.class.conform(args || kwargs)
  raise InvalidValueObjectError.new(result) unless result.passed?

  super(result.value.freeze)
end

Class Method Details

.conform(value) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/definition/value_object.rb', line 25

def conform(value)
  unless @definition
    raise Definition::NotConfiguredError.new(
      "Value object has not been configured with a defintion. Use .definition to set a definition"
    )
  end

  @definition.conform(value)
end

.define_accessor_methodsObject



40
41
42
43
44
# File 'lib/definition/value_object.rb', line 40

def define_accessor_methods
  @definition.keys.each do |key|
    define_method(key) { self[key] }
  end
end

.definition(definition) ⇒ Object



35
36
37
38
# File 'lib/definition/value_object.rb', line 35

def definition(definition)
  @definition = definition
  define_accessor_methods if definition.is_a?(Definition::Types::Keys)
end