Sha256: ee1d4b04b8ba00d42f5b4f9a72454bf3474bb8aa4143a5718408f9974e567380

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Dry::Initializer
  # Immutable container for chunks of code describing argument signatures.
  # Responcible for building the resulting signature for the initializer args.
  class Signature
    include Enumerable
    include Errors

    def initialize
      @list = []
    end

    def add(*args)
      signature = Plugins::Signature.new(*args)

      validates_uniqueness_of signature
      validates_order_of signature

      copy { @list += [signature] }
    end

    def each
      (@list.select(&:param?) + @list.reject(&:param?)).each do |item|
        yield item
      end
    end

    def call
      map(&:call).join(", ")
    end

    private

    def copy(&block)
      dup.tap do |instance|
        instance.instance_eval(&block)
      end
    end

    def validates_uniqueness_of(signature)
      return unless include? signature

      fail RedefinitionError.new(signature.name)
    end

    def validates_order_of(signature)
      return unless signature.param? && !signature.optional?
      return unless any? { |item| item.param? && item.optional? }

      fail OrderError.new(signature.name)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-initializer-0.7.0 lib/dry/initializer/signature.rb
dry-initializer-0.6.0 lib/dry/initializer/signature.rb
dry-initializer-0.5.0 lib/dry/initializer/signature.rb