Class: UserChoices::AbstractArglistStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/user-choices/arglist-strategies.rb

Overview

Arglists cause complications, mainly because a command’s arglist is never optional. If you ever want it to be ignored, for example, you have to treat it specially. An AbstractArglistStrategy is a sequence of messages that can cope with those sort of complications. These messages are called at the appropriate time by a CommandLineSource.

  • AbstractArglistStrategy#fill takes the arglist and converts it to the value of some choice symbol. The name should remind you of AbstractSource#fill.

  • There may be conversions that make sense for values (for this choice symbol) when those values do not come from an arglist, but not when they do. AbstractArglistStrategy#claim_conversions squirrels them away to protect them from more generic processing. They are then specially processed by AbstractArglistStrategy#apply_claimed_conversions.

  • After conversions, there may still be work to do. There may be some special reconciling required to the entire collection of choices. (The final result may depend on what value the arglist provided and what value some other source provided.) AbstractArglistStrategy#adjust does that work.

Direct Known Subclasses

ArbitraryArglist, NoArguments, NonListStrategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_holder, choice = nil) ⇒ AbstractArglistStrategy

A strategy applies an argument list named choice that is a key in the value_holder. It’s hackish, but don’t give the choice in the case where there should be no arglist (and thus no choice symbol to attach it to).



35
36
37
38
# File 'lib/user-choices/arglist-strategies.rb', line 35

def initialize(value_holder, choice=nil)
  @value_holder = value_holder
  @choice = choice
end

Instance Attribute Details

#choiceObject (readonly)

:nodoc:



29
30
31
# File 'lib/user-choices/arglist-strategies.rb', line 29

def choice
  @choice
end

Instance Method Details

#adjust(all_choices) ⇒ Object

Apply any effects of changes to the arglist to the result for all the choices.



56
57
58
# File 'lib/user-choices/arglist-strategies.rb', line 56

def adjust(all_choices)
  # By default, do nothing.
end

#apply_claimed_conversionsObject

Apply the claimed conversions to the value previously stored in claim_conversions.



51
52
53
# File 'lib/user-choices/arglist-strategies.rb', line 51

def apply_claimed_conversions
  # None claimed by default
end

#arglist_arity_error(length, arglist_arity) ⇒ Object

public for testing.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/user-choices/arglist-strategies.rb', line 61

def arglist_arity_error(length, arglist_arity) # :nodoc:
  plural = length==1 ? '' : 's'
  expected = case arglist_arity
    when Integer
      arglist_arity.to_s
    when Range
      if arglist_arity.end == arglist_arity.begin.succ
        "#{arglist_arity.begin} or #{arglist_arity.end}"
      else
        arglist_arity.in_words
      end
    else
      arglist_arity.inspect
    end
  "#{length} argument#{plural} given, #{expected} expected."
end

#claim_conversions(conversions_map) ⇒ Object

Given conversions_map, a list of Conversion, select which apply to the arglist, removing them from the hash.



46
47
48
# File 'lib/user-choices/arglist-strategies.rb', line 46

def claim_conversions(conversions_map) 
  @claimed_conversions = []
end

#fill(arglist) ⇒ Object

This method takes the argument list, an array, and puts it into the value_holder.



42
# File 'lib/user-choices/arglist-strategies.rb', line 42

def fill(arglist); subclass_responsibility; end