module Depth::Enumeration::Enumerable

Public Instance Methods

base() click to toggle source

:nocov:

# File lib/depth/enumeration/enumerable.rb, line 5
def base
  raise NoMethodError.new('should be overridden')
end
each(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 83
def each(&block)
  enumerate { |node, route| block.call(node.parent_key, node.fragment, route) }
end
each_with_object(object, &block) click to toggle source

:nocov:

# File lib/depth/enumeration/enumerable.rb, line 10
def each_with_object(object, &block)
  object.tap do |obj|
    each do |key, fragment, route|
      block.call(key, fragment, obj, route)
    end
  end
end
map(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 74
def map(&block)
  node_map do |node, new_q, route|
    orig_key = node.parent_key
    existing = new_q.find(node.route)
    orig_fragment = existing.nil? ? node.fragment : existing
    block.call(orig_key, orig_fragment, route)
  end
end
map!(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 57
def map!(&block)
  @base = map(&block).base
  self
end
map_keys(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 62
def map_keys(&block)
  map do |key, fragment, route|
    [block.call(key, route), fragment]
  end
end
map_keys!(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 47
def map_keys!(&block)
  @base = map_keys(&block).base
  self
end
map_values(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 68
def map_values(&block)
  map do |key, fragment, route|
    [key, block.call(fragment, route)]
  end
end
map_values!(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 52
def map_values!(&block)
  @base = map_values(&block).base
  self
end
reduce(memo, &block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 40
def reduce(memo, &block)
  each do |key, fragment, route|
    memo = block.call(memo, key, fragment, route)
  end
  memo
end
reject(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 36
def reject(&block)
  select{ |key, fragment, route| !block.call(key, fragment, route) }
end
select(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 18
def select(&block)
  new_q = self.class.new(base.class.new)
  routes_to_delete = []
  enumerate do |node, route|
    key = node.parent_key
    existing = new_q.find(node.route)
    fragment = existing.nil? ? node.fragment : existing
    keep = block.call(key, fragment, route)
    if keep
      new_q.alter(node.route, key: key, value: fragment)
    else
      routes_to_delete << node.route
    end
  end
  routes_to_delete.each { |r| new_q.delete(r) }
  new_q
end

Private Instance Methods

enumerate() { |current, humanized_route| ... } click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 98
def enumerate
  root = Node.new(nil, nil, base)
  current = root
  begin
    if current.next?
      current = current.next
    elsif !current.root?
      yield(current, current.humanized_route)
      current = current.parent
    end
  end while !current.root? || current.next?
  self
end
node_map(&block) click to toggle source
# File lib/depth/enumeration/enumerable.rb, line 89
def node_map(&block)
  new_q = self.class.new(base.class.new)
  enumerate do |node, route|
    key, val = block.call(node, new_q, route)
    new_q.alter(node.route, key: key, value: val)
  end
  new_q
end