Class: Af::OptionParser::InstanceVariableSetter

Inherits:
Object
  • Object
show all
Defined in:
lib/fiksu-af/option_parser/instance_variable_setter.rb

Direct Known Subclasses

Option, OptionCheck, OptionSelect

Constant Summary collapse

FACTORY_SETTABLES =
[
  :evaluation_method,
  :hidden,
  :value_to_set_target_variable,
  :do_not_create_accessor,
  :target_variable,
  :target_container,
  :disabled
]

Instance Method Summary collapse

Constructor Details

#initialize(parameters = {}) ⇒ InstanceVariableSetter

Returns a new instance of InstanceVariableSetter.



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

def initialize(parameters = {})
  set_instance_variables(parameters)
  @target_container ||= :af_application
end

Instance Method Details

#evaluate(argument) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 47

def evaluate(argument)
  if has_value_to_set_target_variable?
    argument = @value_to_set_target_variable
  else
    if @requirements == ::Af::OptionParser::GetOptions::NO_ARGUMENT
      argument = true
    end
  end

  evaluator = @evaluation_method ||
    @option_type ||
    OptionType.find_by_value(argument) ||
    OptionType.find_by_short_name(:switch)

  if evaluator.nil?
    raise UndeterminedArgumentTypeError.new(@long_name)
  elsif evaluator.is_a? Proc
    return evaluator.call(argument, self)
  else
    return evaluator.evaluate_argument(argument, self)
  end
end

#evaluate_and_set_target(argument) ⇒ Object



42
43
44
45
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 42

def evaluate_and_set_target(argument)
  value = evaluate(argument)
  set_target_variable(value)
end

#has_value_to_set_target_variable?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 38

def has_value_to_set_target_variable?
  return @has_value_to_set_target_variable || false
end

#instantiate_target_variableObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 70

def instantiate_target_variable
  if target_container.present? && target_variable.present?
    set_target_variable(@default_value)
    unless @do_not_create_accessor
      if target_container.is_a? Class
        target_container.class_eval "def self.#{target_variable}; return #{target_class_variable}; end"
        target_container.class_eval "def self.#{target_variable}=(value); return #{target_class_variable} = value; end"
      else
        target_container.class.class_eval "attr_accessor :#{target_variable}"
      end
    end
  end
end

#merge(that_option, other_settables = []) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 108

def merge(that_option, other_settables = [])
  (FACTORY_SETTABLES + other_settables).each do |name|
    if that_option.instance_variable_defined?("@#{name}")
      self.send("#{name}=", that_option.send(name))
    end
  end
end

#set_instance_variables(parameters = {}, other_settables = []) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 96

def set_instance_variables(parameters = {}, other_settables = [])
  parameters.select do |name, value|
    (FACTORY_SETTABLES + other_settables).include? name
  end.each do |name, value|
    instance_variable_set("@#{name}", value)
  end

  if parameters[:value_to_set_target_variable]
    @has_value_to_set_target_variable = true
  end
end

#set_target_variable(value) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 84

def set_target_variable(value)
  if target_container.present? && target_variable.present?
    if target_container.is_a? Class
      # this is a Class -- set @@foo
      target_container.class_variable_set(target_class_variable, value)
    else
      # this is an instance -- set @foo
      target_container.instance_variable_set(target_instance_variable, value)
    end
  end
end

#target_class_variableObject



30
31
32
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 30

def target_class_variable
  return "@@#{target_variable}"
end

#target_containerObject


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



23
24
25
26
27
28
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 23

def target_container
  if @target_container == :af_application
    @target_container = ::Af::Application.singleton
  end
  return @target_container
end

#target_instance_variableObject



34
35
36
# File 'lib/fiksu-af/option_parser/instance_variable_setter.rb', line 34

def target_instance_variable
  return "@#{target_variable}"
end