Class: Holotype::ValueNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/holotype/value_normalizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ ValueNormalizer

Returns a new instance of ValueNormalizer.



5
6
7
# File 'lib/holotype/value_normalizer.rb', line 5

def initialize definition
  @definition = definition
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



3
4
5
# File 'lib/holotype/value_normalizer.rb', line 3

def definition
  @definition
end

Instance Method Details

#normalize(value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/holotype/value_normalizer.rb', line 9

def normalize value
  result = if definition.has_value_class?
    if value.nil?
      nil
    else
      definition.value_class.new(**value)
    end
  else
    value
  end

  if definition.immutable?
    result.freeze
  else
    result
  end
end