Class: EcoRake::Options::Forwarding::Rule
- Inherits:
-
Object
- Object
- EcoRake::Options::Forwarding::Rule
- 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
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
-
#sym ⇒ Object
Returns the value of attribute sym.
Instance Method Summary collapse
- #active?(option_results) ⇒ Boolean
-
#apply(option_results) ⇒ NilClass, Value
The value that the rule returns,
nil
if rule inactive/not-applicable. - #desc ⇒ Object
-
#dup(parent: nil) ⇒ EcoRake::Options::Forwarding::Rule
(also: #deep_dup)
Does a copy of this rule.
-
#initialize(sym, rule, parent: nil) ⇒ Rule
constructor
A new instance of Rule.
-
#option ⇒ EcoRake::Option
The option associated with
sym
name. -
#option_ref ⇒ Object
Reference of the associated option.
- #results_value(option_results) ⇒ Object
Methods included from Base::MethodHelpers
Constructor Details
#initialize(sym, rule, parent: nil) ⇒ Rule
Returns a new instance of Rule.
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
#parent ⇒ Object
Returns the value of attribute parent.
10 11 12 |
# File 'lib/eco-rake/options/forwarding/rule.rb', line 10 def parent @parent end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
11 12 13 |
# File 'lib/eco-rake/options/forwarding/rule.rb', line 11 def rule @rule end |
#sym ⇒ Object
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
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:
- Retrives value from parsed options results by using the option name, short or sym.
- It then applies the rule on it.
Returns 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 |
#desc ⇒ Object
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 |
#option ⇒ EcoRake::Option
Returns the option associated with sym
name.
76 77 78 |
# File 'lib/eco-rake/options/forwarding/rule.rb', line 76 def option parent&.send(:option, sym) end |
#option_ref ⇒ Object
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 |