Class: Dotpath::Walk

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

Class Method Summary collapse

Class Method Details

.array(obj, path: DELIMITER, &block) ⇒ Object



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

def self.array(obj, path: DELIMITER, &block)
  index = 0
  obj.each do |value|
    new_path = "#{path}[#{index}]"
    case value
    when Hash
      hash(value, path: new_path, &block)
    when Array
      array(value, path: new_path, &block)
    else
      yield(new_path, value)
    end

    index += 1
  end
end

.hash(obj, path: DELIMITER, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dotpath/walk.rb', line 16

def self.hash(obj, path: DELIMITER, &block)
  obj.each do |key, value|
    new_path = [path, Dotpath.encode(key)].join(DELIMITER)
    case value
    when Hash
      hash(value, path: new_path, &block)
    when Array
      array(value, path: new_path, &block)
    else
      yield(new_path, value)
    end
  end
end

.walk(obj, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/dotpath/walk.rb', line 5

def self.walk(obj, &block)
  case obj
  when Hash
    hash(obj, path: '', &block)
  when Array
    array(obj, &block)
  else
    obj
  end
end