class Composed::Positional

Constants

STRATEGY_LOOKUP

Public Class Methods

new(merge_strategy = default_strategy) click to toggle source
# File lib/composed/positional.rb, line 4
def initialize(merge_strategy = default_strategy)
  @injected = {}
  @index = 0
  @merge_strategy = merge_strategy
end

Private Class Methods

klass_for(strategy) click to toggle source
# File lib/composed/positional.rb, line 82
      def klass_for(strategy)
        return strategy if strategy.respond_to?(:new)

        STRATEGY_LOOKUP.fetch(strategy) { raise ArgumentError, <<~ERROR }
          Unsupported strategy: #{strategy}. Use one of the following:
          :skip - Skips over injected arguments. Cannot override injections at call time.
          :override - Positional arguments behave as normal function calls. Defaults are overridden in order.
          :default - Set the default. :override
        ERROR

      end
strategy() click to toggle source
# File lib/composed/positional.rb, line 95
def self.strategy
  @strategy ||= klass_for(:default).new
end
strategy=(strategy) click to toggle source
# File lib/composed/positional.rb, line 78
def strategy=(strategy)
  @strategy = klass_for(strategy).new
end

Public Instance Methods

<<(value)
Alias for: push
[]=(idx, value) click to toggle source
# File lib/composed/positional.rb, line 10
def []=(idx, value)
  @index = idx
  push(value)
end
merge(args) click to toggle source
# File lib/composed/positional.rb, line 22
def merge(args)
  @merge_strategy.call(args,@injected)
end
push(value) click to toggle source
# File lib/composed/positional.rb, line 15
def push(value)
  set(@index,value)
  @index += 1
end
Also aliased as: <<

Private Instance Methods

default_strategy() click to toggle source
# File lib/composed/positional.rb, line 67
def default_strategy
  self.class.strategy
end
set(idx, value) click to toggle source
# File lib/composed/positional.rb, line 27
def set(idx, value)
  @injected[idx] = value
  @injected = Hash[@injected.sort]
end