Class: Af::OptionParser::OptionCheck

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

Constant Summary collapse

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

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 = {}) ⇒ OptionCheck

Returns a new instance of OptionCheck.



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

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

Instance Attribute Details

#var_nameObject

Returns the value of attribute var_name.



11
12
13
# File 'lib/fiksu-af/option_parser/option_check.rb', line 11

def var_name
  @var_name
end

Instance Method Details

#merge(that_option) ⇒ Object



26
27
28
# File 'lib/fiksu-af/option_parser/option_check.rb', line 26

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

#set_instance_variables(parameters = {}) ⇒ Object


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



22
23
24
# File 'lib/fiksu-af/option_parser/option_check.rb', line 22

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

#validateObject

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

Available actions: requires, excludes

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



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
# File 'lib/fiksu-af/option_parser/option_check.rb', line 37

def validate
  # If an option_check is used, the target_variable must be instantiated
  if target_container.try(target_variable.to_sym).blank?
    raise OptionCheckError.new("Option --#{target_variable} must be specified")
  end

  # If an option_check is used, an array of options must be given
  if targets.empty?
    raise OptionCheckError.new("An array of #{action.to_s[0..-2]}d options must be specified")
  end

  if action == :requires
    # Each target option must be specified
    targets.each do |target|
      if target_container.try(target.to_sym).blank?
        raise OptionCheckError.new("You must specify these options: --#{targets.join(', --')}")
      end
    end
  elsif action == :excludes
    # None of the target options can be specified
    targets.each do |target|
      if target_container.try(target.to_sym).present?
        raise OptionCheckError.new("You cannot specify these options: --#{targets.join(', --')}")
      end
    end
  end
end