Class: EcoRake::Options::Forwarding::Rule

Inherits:
Object
  • Object
show all
Extended by:
Base::MethodHelpers
Defined in:
lib/eco-rake/options/forwarding/rule.rb

Constant Summary collapse

RULE_TYPES =
[String, Symbol, Proc].freeze
SYMBOL_TYPES =
[:mirror].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base::MethodHelpers

aux_curry, safe_call

Constructor Details

#initialize(sym, rule, parent: nil) ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • rule (String, Proc)
    • Proc -> [value, ]

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
# File 'lib/eco-rake/options/forwarding/rule.rb', line 15

def initialize(sym, rule, parent: nil)
  msg = "Rule should be either of: #{RULE_TYPES.join(', ')}. Given: #{rule.class}"
  raise ArgumentError, msg unless RULE_TYPES.any? {|type| rule.is_a?(type)}
  msg = "Accepted Symbol rules are: :#{SYMBOL_TYPES.join(', :')}. Given :#{rule}"
  raise ArgumentError, msg if rule.is_a?(Symbol) && SYMBOL_TYPES.none? {|type| rule == type}
  @parent = parent
  @sym    = sym
  @rule   = rule
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/eco-rake/options/forwarding/rule.rb', line 10

def parent
  @parent
end

#ruleObject (readonly)

Returns the value of attribute rule.



11
12
13
# File 'lib/eco-rake/options/forwarding/rule.rb', line 11

def rule
  @rule
end

#symObject

Returns the value of attribute sym.



10
11
12
# File 'lib/eco-rake/options/forwarding/rule.rb', line 10

def sym
  @sym
end

Instance Method Details

#active?(option_results) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/eco-rake/options/forwarding/rule.rb', line 25

def active?(option_results)
  opt = option
  [opt&.name, opt&.short, sym].compact.any? {|k| option_results.key?(k)}
end

#apply(option_results) ⇒ NilClass, Value

Note:
  1. Retrives value from parsed options results by using the option name, short or sym.
  2. It then applies the rule on it.

Returns the value that the rule returns, nil if rule inactive/not-applicable.

Parameters:

  • option_results (Hash)

    the parsed option results.

Returns:

  • (NilClass, Value)

    the value that the rule returns, nil if rule inactive/not-applicable.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/eco-rake/options/forwarding/rule.rb', line 44

def apply(option_results)
  return nil unless active?(option_results)
  opt = option
  return nil unless opt || rule != :mirror

  value = results_value(option_results)
  return rule                       if value && rule.is_a?(String)
  return nil                        if rule.is_a?(String)
  return opt.mirror(value)          if rule == :mirror

  self.class.safe_call(rule, value, option_results, opt)
end

#descObject



65
66
67
68
# File 'lib/eco-rake/options/forwarding/rule.rb', line 65

def desc
  ref = (opt_ref = option_ref) ? opt_ref : "on #{sym} (no associated option)"
  "rule #{ref}"
end

#dup(parent: nil) ⇒ EcoRake::Options::Forwarding::Rule Also known as: deep_dup

Does a copy of this rule.



59
60
61
62
# File 'lib/eco-rake/options/forwarding/rule.rb', line 59

def dup(parent: nil)
  new_rule = rule.respond_to?(:deep_dup)? rule.deep_dup : rule.dup
  self.class.new(sym, new_rule, parent: parent)
end

#optionEcoRake::Option

Returns the option associated with sym name.

Returns:



76
77
78
# File 'lib/eco-rake/options/forwarding/rule.rb', line 76

def option
  parent&.send(:option, sym)
end

#option_refObject

Reference of the associated option.



71
72
73
# File 'lib/eco-rake/options/forwarding/rule.rb', line 71

def option_ref
  (opt = option) ? "on option '#{opt.name}' (#{opt.short})" : nil
end

#results_value(option_results) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/eco-rake/options/forwarding/rule.rb', line 30

def results_value(option_results)
  opt = option
  value = nil
  value = option_results[opt.name]  if opt && value.nil?
  value = option_results[opt.short] if opt && value.nil?
  value = option_results[sym]       if value.nil?
  value
end