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.



8
9
10
11
12
13
# File 'lib/definition/value_object.rb', line 8

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

  super(result.value.freeze)
end

Class Method Details

.conform(value) ⇒ Object



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

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



31
32
33
34
35
# File 'lib/definition/value_object.rb', line 31

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

.definition(definition) ⇒ Object



26
27
28
29
# File 'lib/definition/value_object.rb', line 26

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