Class: Leafy::FieldValue

Inherits:
Object
  • Object
show all
Defined in:
lib/leafy/field_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ FieldValue

Returns a new instance of FieldValue.



7
8
9
10
11
12
13
14
15
# File 'lib/leafy/field_value.rb', line 7

def initialize(attributes)
  attributes = attributes.dup.to_a.map { |pair| [pair[0].to_sym, pair[1]]}.to_h

  self.id = attributes.fetch(:id)
  self.name = attributes.fetch(:name)
  self.type = attributes.fetch(:type)
  self.converter = attributes.fetch(:converter) { Leafy.converters.fetch(type.to_sym) { raise(RuntimeError, "unregistered converter #{type}") } }
  self.raw = attributes.fetch(:raw)
end

Instance Attribute Details

#converterObject

Returns the value of attribute converter.



5
6
7
# File 'lib/leafy/field_value.rb', line 5

def converter
  @converter
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/leafy/field_value.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/leafy/field_value.rb', line 5

def name
  @name
end

#rawObject

Returns the value of attribute raw.



5
6
7
# File 'lib/leafy/field_value.rb', line 5

def raw
  @raw
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/leafy/field_value.rb', line 5

def type
  @type
end

Instance Method Details

#valueObject



17
18
19
# File 'lib/leafy/field_value.rb', line 17

def value
  converter.load(raw)
end

#value=(val) ⇒ Object



21
22
23
# File 'lib/leafy/field_value.rb', line 21

def value=(val)
  self.raw = converter.dump(converter.load(val))
end