Module: Depth::Enumeration::Enumerable

Included in:
ComplexHash
Defined in:
lib/depth/enumeration/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#baseObject

:nocov:

Raises:

  • (NoMethodError)


5
6
7
# File 'lib/depth/enumeration/enumerable.rb', line 5

def base
  raise NoMethodError.new('should be overridden')
end

#each(&block) ⇒ Object



61
62
63
# File 'lib/depth/enumeration/enumerable.rb', line 61

def each(&block)
  enumerate { |node| block.call(node.parent_key, node.fragment) }
end

#each_with_object(object, &block) ⇒ Object

:nocov:



10
11
12
13
14
15
16
# File 'lib/depth/enumeration/enumerable.rb', line 10

def each_with_object(object, &block)
  object.tap do |o|
    each do |key, fragment|
      block.call(key, fragment, o)
    end
  end
end

#map(&block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/depth/enumeration/enumerable.rb', line 52

def map(&block)
  node_map do |node, new_q|
    orig_key = node.parent_key
    existing = new_q.find(node.route)
    orig_fragment = existing.nil? ? node.fragment : existing
    block.call(orig_key, orig_fragment)
  end
end

#map!(&block) ⇒ Object



35
36
37
38
# File 'lib/depth/enumeration/enumerable.rb', line 35

def map!(&block)
  @base = map(&block).base
  self
end

#map_keys(&block) ⇒ Object



40
41
42
43
44
# File 'lib/depth/enumeration/enumerable.rb', line 40

def map_keys(&block)
  map do |key, fragment|
    [block.call(key), fragment]
  end
end

#map_keys!(&block) ⇒ Object



25
26
27
28
# File 'lib/depth/enumeration/enumerable.rb', line 25

def map_keys!(&block)
  @base = map_keys(&block).base
  self
end

#map_values(&block) ⇒ Object



46
47
48
49
50
# File 'lib/depth/enumeration/enumerable.rb', line 46

def map_values(&block)
  map do |key, fragment, parent_type|
    [key, block.call(fragment)]
  end
end

#map_values!(&block) ⇒ Object



30
31
32
33
# File 'lib/depth/enumeration/enumerable.rb', line 30

def map_values!(&block)
  @base = map_values(&block).base
  self
end

#reduce(memo, &block) ⇒ Object



18
19
20
21
22
23
# File 'lib/depth/enumeration/enumerable.rb', line 18

def reduce(memo, &block)
  each do |key, fragment|
    memo = block.call(memo, key, fragment)
  end
  memo
end