Class: Hashing::Ivar
- Inherits:
-
Object
- Object
- Hashing::Ivar
- Defined in:
- lib/hashing/ivar.rb
Overview
Represents each one of the instance variables in a class that should be used to represent an object in a ‘Hash` form (serialization).
Instance Attribute Summary collapse
-
#from_hash(value, metadata = {}) ⇒ Object
Processes the Object provinient from a ‘Hash` so it can be used to reconstruct an instance.
- #name ⇒ Object readonly
-
#to_h(value) ⇒ Object
Processes the parameter acordingly to the configuration made in the constructor.
Instance Method Summary collapse
-
#initialize(name, to_h = nil, from_hash = nil) ⇒ Ivar
constructor
Configure the name of an ‘ivar` and the ’callable’ objects thath will be used to prepare the ‘ivar` value for serialization, and to load the object from a `Hash`.
- #to_s ⇒ Object
- #to_sym ⇒ Object
Constructor Details
#initialize(name, to_h = nil, from_hash = nil) ⇒ Ivar
Configure the name of an ‘ivar` and the ’callable’ objects thath will be used to prepare the ‘ivar` value for serialization, and to load the object from a `Hash`.
17 18 19 20 21 |
# File 'lib/hashing/ivar.rb', line 17 def initialize(name, to_h = nil, from_hash = nil) @name = name.to_sym @to_h = to_h @from_hash = from_hash end |
Instance Attribute Details
#from_hash(value, metadata = {}) ⇒ Object
Processes the Object provinient from a ‘Hash` so it can be used to reconstruct an instance.
42 43 44 45 46 47 48 49 |
# File 'lib/hashing/ivar.rb', line 42 def from_hash(value, = {}) return value unless @from_hash if value.respond_to? :map value = normalize(value, ) end @from_hash.call value end |
#name ⇒ Object (readonly)
7 8 9 |
# File 'lib/hashing/ivar.rb', line 7 def name @name end |
#to_h(value) ⇒ Object
Processes the parameter acordingly to the configuration made in the constructor. If some was made, return the value after being processed or else return the value as it is.
Also guarantee that if a value is a #map, every item with ‘Hashing` in his method lookup will be sent the message `#to_h`.
32 33 34 35 |
# File 'lib/hashing/ivar.rb', line 32 def to_h(value) return value unless @to_h @to_h.call value end |
Instance Method Details
#to_s ⇒ Object
55 56 57 |
# File 'lib/hashing/ivar.rb', line 55 def to_s @name.to_s end |
#to_sym ⇒ Object
51 52 53 |
# File 'lib/hashing/ivar.rb', line 51 def to_sym @name.to_sym end |