Class: HashieWalker

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|@multiblock| ... } ⇒ HashieWalker

Returns a new instance of HashieWalker.

Yields:

  • (@multiblock)


4
5
6
7
# File 'lib/hashie_walker.rb', line 4

def initialize
  @multiblock = Multiblock.wrapper { |obj| obj }
  yield(@multiblock) if block_given?
end

Class Method Details

.walk(object, &blk) ⇒ Object



27
28
29
30
# File 'lib/hashie_walker.rb', line 27

def walk(object, &blk)
  return object unless block_given?
  new(&blk).walk(object)
end

Instance Method Details

#walk(object) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hashie_walker.rb', line 9

def walk(object)
  case object
  when Hash
    object.inject({}) do |hash, (k, v)|
      hash[@multiblock.call(:key, k)] = walk(v)
      hash
    end
  when Array
    object.inject([]) do |arr, element|
      arr << walk(element)
      arr
    end
  else
    @multiblock.call(:value, object)
  end
end