Sha256: 306bb251b24219ab76ccd112e602c40e7e69966fa59c02c5f5f1321baf587fec

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module Depth
  module Enumerable

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

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

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

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

    def map_keys_and_values!(&block)
      @base = map_keys_and_values(&block).base
    end

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

    # Convention is that only values are mapped
    def map(&block)
      map_keys_and_values do |key, fragment|
        [key, block.call(fragment)]
      end
    end

    def map_keys_and_values(&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
        next [orig_key, orig_fragment] unless node.parent.hash?
        block.call(orig_key, orig_fragment)
      end
    end

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

    private

    def node_map(&block)
      new_q = ComplexHash.new(base.class.new)
      enumerate do |node|
        key, val = block.call(node, new_q)
        new_q.alter(node.route, key: key, value: val)
      end
      new_q
    end

    def enumerate
      root = Node.new(nil, nil, query)
      current = root
      begin
        if current.next?
          current = current.next
        elsif !current.root?
          yield(current)
          current = current.parent
        end
      end while !current.root? || current.next?
      root.fragment
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
depth-0.0.1 lib/depth/enumeration/enumerable.rb