Sha256: 878d032ee0bbb5a053d01d486888ddc1e7db0536fba04917354bf9b3d9d06eb4

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Clowne
  class Plan # :nodoc: all
    class TwoPhaseSet
      def initialize
        @added = {}
        @removed = []
      end

      def []=(k, v)
        return if @removed.include?(k)
        @added[k] = v
      end

      def delete(k)
        return if @removed.include?(k)
        @removed << k
        @added.delete(k)
      end

      def values
        @added.values
      end
    end

    def initialize(registry)
      @registry = registry
      @data = {}
    end

    def add(type, declaration)
      data[type] = [] unless data.key?(type)
      data[type] << declaration
    end

    def add_to(type, id, declaration)
      data[type] = TwoPhaseSet.new unless data.key?(type)
      data[type][id] = declaration
    end

    def set(type, declaration)
      data[type] = declaration
    end

    def get(type)
      data[type]
    end

    def remove(type)
      data.delete(type)
    end

    def remove_from(type, id)
      return unless data[type]
      data[type].delete(id)
    end

    def declarations
      registry.actions.flat_map do |type|
        value = data[type]
        next if value.nil?
        value = value.values if value.is_a?(TwoPhaseSet)
        value = Array(value)
        value.map { |v| [type, v] }
      end.compact
    end

    def dup
      self.class.new(registry).tap do |duped|
        data.each do |k, v|
          duped.set(k, v.dup)
        end
      end
    end

    private

    attr_reader :data, :registry
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
clowne-0.1.0 lib/clowne/plan.rb
clowne-0.1.0.beta1 lib/clowne/plan.rb
clowne-0.1.0.pre1 lib/clowne/plan.rb