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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#from_hash(value, metadata = {}) ⇒ Object
Processes the Object provinient from a ‘Hash` so it can be used to reconstruct an instance.
-
#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_h(value) ⇒ Object
Processes the parameter acordingly to the configuration made in the constructor.
- #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`.
14 15 16 17 18 |
# File 'lib/hashing/ivar.rb', line 14 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
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/hashing/ivar.rb', line 5 def name @name end |
Instance Method Details
#from_hash(value, metadata = {}) ⇒ Object
Processes the Object provinient from a ‘Hash` so it can be used to reconstruct an instance.
43 44 45 46 47 48 49 50 |
# File 'lib/hashing/ivar.rb', line 43 def from_hash(value, = {}) return value unless @from_hash if value.respond_to? :map value = normalize(value, ) end @from_hash.call value 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`.
29 30 31 32 33 34 35 36 |
# File 'lib/hashing/ivar.rb', line 29 def to_h(value) return value unless @to_h if value.respond_to? :map value = hasherize value end @to_h.call value end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/hashing/ivar.rb', line 56 def to_s @name.to_s end |
#to_sym ⇒ Object
52 53 54 |
# File 'lib/hashing/ivar.rb', line 52 def to_sym @name.to_sym end |