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
24
# 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)


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

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. Retrieves 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.



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

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



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

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.



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

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:



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

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

#option_refObject

Reference of the associated option.



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

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

#results_value(option_results) ⇒ Object



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

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