Sha256: d859b53bd8355baf9c2f70020b85553689cc56df24a88b04562c6a7064479ad9

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require "forwardable"
require "terrestrial/short_inspection_string"

module Terrestrial
  class CollectionMutabilityProxy
    extend Forwardable
    include ShortInspectionString
    include Enumerable

    def initialize(collection)
      @collection = collection
      @added_nodes = []
      @deleted_nodes = []
    end

    attr_reader :collection, :deleted_nodes, :added_nodes
    private     :collection, :deleted_nodes, :added_nodes

    def_delegators :collection, :where, :subset

    def each_loaded(&block)
      loaded_enum.each(&block)
    end

    def each_deleted(&block)
      @deleted_nodes.each(&block)
    end

    def to_ary
      to_a
    end

    def each(&block)
      if block
        enum.each(&block)
        self
      else
        enum
      end
    end

    def delete(node)
      @deleted_nodes.push(node)
      self
    end

    def push(node)
      @added_nodes.push(node)
    end

    private

    def loaded_enum
      Enumerator.new do |yielder|
        collection.each_loaded do |element|
          yielder.yield(element) unless deleted?(element)
        end

        added_nodes.each do |node|
          yielder.yield(node)
        end
      end
    end

    def enum
      Enumerator.new do |yielder|
        collection.each do |element|
          yielder.yield(element) unless deleted?(element)
        end

        added_nodes.each do |node|
          yielder.yield(node)
        end
      end
    end

    def deleted?(node)
      @deleted_nodes.include?(node)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terrestrial-0.1.1 lib/terrestrial/collection_mutability_proxy.rb