Class: Composed::Positional

Inherits:
Object
  • Object
show all
Defined in:
lib/composed/positional.rb

Defined Under Namespace

Classes: OverrideStrategy, SkipStrategy

Constant Summary collapse

STRATEGY_LOOKUP =
{
  skip: SkipStrategy,
  override: OverrideStrategy,
  default: OverrideStrategy
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(merge_strategy = default_strategy) ⇒ Positional

Returns a new instance of Positional.



4
5
6
7
8
# File 'lib/composed/positional.rb', line 4

def initialize(merge_strategy = default_strategy)
  @injected = {}
  @index = 0
  @merge_strategy = merge_strategy
end

Class Method Details

.klass_for(strategy) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/composed/positional.rb', line 82

def klass_for(strategy)
  return strategy if strategy.respond_to?(:new)

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

.strategy=(strategy) ⇒ Object



78
79
80
# File 'lib/composed/positional.rb', line 78

def strategy=(strategy)
  @strategy = klass_for(strategy).new
end

Instance Method Details

#[]=(idx, value) ⇒ Object



10
11
12
13
# File 'lib/composed/positional.rb', line 10

def []=(idx, value)
  @index = idx
  push(value)
end

#merge(args) ⇒ Object



22
23
24
# File 'lib/composed/positional.rb', line 22

def merge(args)
  @merge_strategy.call(args,@injected)
end

#push(value) ⇒ Object Also known as: <<



15
16
17
18
# File 'lib/composed/positional.rb', line 15

def push(value)
  set(@index,value)
  @index += 1
end