Class: HashTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/ascii_invoicer/hash_transformer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashTransformer

Returns a new instance of HashTransformer.



7
8
9
10
11
12
13
# File 'lib/ascii_invoicer/hash_transformer.rb', line 7

def initialize(hash = {})
  @original_hash = hash[:original_hash]
  @original_hash ||= {}
  @new_hash = {}
  @rules = hash[:rules]
  @rules ||= []
end

Instance Attribute Details

#new_hashObject (readonly)

Returns the value of attribute new_hash.



4
5
6
# File 'lib/ascii_invoicer/hash_transformer.rb', line 4

def new_hash
  @new_hash
end

#original_hashObject (readonly)

Returns the value of attribute original_hash.



4
5
6
# File 'lib/ascii_invoicer/hash_transformer.rb', line 4

def original_hash
  @original_hash
end

#rules=(value) ⇒ Object (writeonly)

Sets the attribute rules

Parameters:

  • value

    the value to set the attribute rules to.



5
6
7
# File 'lib/ascii_invoicer/hash_transformer.rb', line 5

def rules=(value)
  @rules = value
end

Instance Method Details

#transformObject



15
16
17
18
19
20
21
# File 'lib/ascii_invoicer/hash_transformer.rb', line 15

def transform
  @new_hash = @original_hash
  @rules.each {|rule|
    @new_hash.set_path(rule[:new],  @original_hash.get_path(rule[:old]))
  }
  return @new_hash
end