Class: Hashing::Ivar

Inherits:
Object
  • Object
show all
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).

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Since:

  • 0.0.1



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.

Since:

  • 0.0.1



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

#nameObject (readonly)

Since:

  • 0.0.1



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.

Since:

  • 0.0.1



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_sObject

Since:

  • 0.0.1



55
56
57
# File 'lib/hashing/ivar.rb', line 55

def to_s
  @name.to_s
end

#to_symObject

Since:

  • 0.0.1



51
52
53
# File 'lib/hashing/ivar.rb', line 51

def to_sym
  @name.to_sym
end