Class: PathGenerator

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

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ PathGenerator

Returns a new instance of PathGenerator.



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

def initialize(obj)
  @__object = obj
end

Instance Method Details

#pathsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/path_generator.rb', line 8

def paths
  iter = lambda do |o, path, &block|
    if is_empty?(o); return block.call(path) end
    case o
    when String, Symbol then block.call(path + [o])
    when Hash  then o.each { |k, v| iter.call(v, path + [k], &block) unless is_empty?(k) }
    when Array then o.each { |v| iter.call(v, path, &block) unless is_empty?(v) }
    end
  end
  Enumerator.new do |yielder|
    iter.call(@__object, []) { |path| yielder.yield(path) } unless is_empty?(@__object)
  end
end