Class: HTransform

Inherits:
Object
  • Object
show all
Defined in:
lib/htransform.rb,
lib/htransform/version.rb

Constant Summary collapse

ValueNotPresentError =
Class.new(StandardError)
VERSION =
"0.9.0"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTransform

Returns a new instance of HTransform.



23
24
25
26
27
28
# File 'lib/htransform.rb', line 23

def initialize
  @output_hash = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
  @input_nest = []
  @output_nest = []
  @inputs = Set.new
end

Class Attribute Details

.input_nestObject (readonly)

Returns the value of attribute input_nest.



10
11
12
# File 'lib/htransform.rb', line 10

def input_nest
  @input_nest
end

.output_nestObject (readonly)

Returns the value of attribute output_nest.



11
12
13
# File 'lib/htransform.rb', line 11

def output_nest
  @output_nest
end

.transform_blockObject (readonly)

Returns the value of attribute transform_block.



8
9
10
# File 'lib/htransform.rb', line 8

def transform_block
  @transform_block
end

Class Method Details

.convert(input_hash) ⇒ Object



13
14
15
# File 'lib/htransform.rb', line 13

def convert(input_hash)
  new.convert(input_hash)
end

.transform(&trans_block) ⇒ Object



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

def transform(&trans_block)
  @transform_block = trans_block
end

Instance Method Details

#convert(input) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/htransform.rb', line 30

def convert(input)

  unless input.is_a? Hash
    if input.respond_to? :to_hash
      input = input.to_hash
    else
      raise ArgumentError "object is not a Hash or does not respond to to_hash"
    end
  end

  @input_hash = input
  instance_exec(&self.class.transform_block)
  Hash[@output_hash]
end