Class: Af::OptionParser::OptionSelect

Inherits:
InstanceVariableSetter show all
Defined in:
lib/fiksu-af/option_parser/option_select.rb

Constant Summary collapse

FACTORY_SETTABLES =
[
  :action,
  :targets,
  :error_message
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from InstanceVariableSetter

#evaluate, #evaluate_and_set_target, #has_value_to_set_target_variable?, #instantiate_target_variable, #set_target_variable, #target_class_variable, #target_container, #target_instance_variable

Constructor Details

#initialize(var_name, parameters = {}) ⇒ OptionSelect

Returns a new instance of OptionSelect.



12
13
14
15
# File 'lib/fiksu-af/option_parser/option_select.rb', line 12

def initialize(var_name, parameters = {})
  super(parameters)
  @var_name = var_name
end

Instance Attribute Details

#var_nameObject

Returns the value of attribute var_name.



10
11
12
# File 'lib/fiksu-af/option_parser/option_select.rb', line 10

def var_name
  @var_name
end

Instance Method Details

#merge(that_option) ⇒ Object



25
26
27
# File 'lib/fiksu-af/option_parser/option_select.rb', line 25

def merge(that_option)
  super(that_option, FACTORY_SETTABLES)
end

#set_instance_variables(parameters = {}) ⇒ Object


*** Instance Methods *** +++++++++++++++++++++++++



21
22
23
# File 'lib/fiksu-af/option_parser/option_select.rb', line 21

def set_instance_variables(parameters = {})
  super(parameters, FACTORY_SETTABLES)
end

#validateObject

This methods validates the selected options based on the chosen action.

Available actions: one_of, none_or_one_of, one_or_more_of

If an invalidation occurs, an OptionSelectError is raised with a specific message.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fiksu-af/option_parser/option_select.rb', line 36

def validate
  # If an option_select is used, an array of options must be given
  if targets.blank?
    raise OptionSelectError.new("An array of options must be specified")
  end

  # Populate the options_set array with all instantiated class/instance variables
  options_set = []
  targets.each do |target|
    if target_container.try(target.to_sym).present?
      options_set << target
    end
  end

  # Assign instantiated options to an instance variable
  if options_set.size == 1
    value = options_set.first
  else
    value = options_set
  end
  target_container.instance_variable_set("@#{target_variable}", value)

  if action == :one_of && options_set.size != 1
    raise OptionSelectError.new("You must specify only one of these options: --#{targets.join(', --')}")
  elsif action == :none_or_one_of && options_set.size > 1
    raise OptionSelectError.new("You must specify no more than one of these options: --#{targets.join(', --')}")
  elsif action == :one_or_more_of && options_set.size < 1
    raise OptionSelectError.new("You must specify at least one of these options: --#{targets.join(', --')}")
  end
end