Class: EcoRake::Options::Forwarding::Proxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/eco-rake/options/forwarding/proxy.rb

Overview

Note:

rules keep associated with the option name (not their short)

  • The above means that changing the name of an exisint option will brake the link with a forwarding rule (as they do not auto-update on option name change)

Collection of forwarding rules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • parent (RakeCommander::Options::Class)

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
# File 'lib/eco-rake/options/forwarding/proxy.rb', line 14

def initialize(parent)
  pklass = parent.is_a?(Class)? parent : parent.class
  msg    = "Expecting parent to be RakeCommander::Options::Class. Given: #{pklass}"
  raise ArgumentError, msg unless parent.is_a?(Class) && parent <= RakeCommander::Options
  @parent = parent
  @rules  = {}
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#[](value) ⇒ EcoRake::Options::Forwarding::Rule



36
37
38
# File 'lib/eco-rake/options/forwarding/proxy.rb', line 36

def [](value)
  @rules[key(value)]
end

#add(sym, rule, override: true, require_option: true) ⇒ Object

Adds a new rule or overrides an existing one.

See Also:

  • #add_rule


42
43
44
45
46
47
48
# File 'lib/eco-rake/options/forwarding/proxy.rb', line 42

def add(sym, rule, override: true, require_option: true)
  add_rule(
    EcoRake::Options::Forwarding::Rule.new(key(sym) || sym, rule, parent: self),
    override:       override,
    require_option: require_option
  )
end

#delete(ref) ⇒ EcoRake::Options::Forwarding::Rule

Returns the rule that has been deleted.

Parameters:

Returns:



52
53
54
# File 'lib/eco-rake/options/forwarding/proxy.rb', line 52

def delete(ref)
  @rules.delete(key(ref))
end

#dup(parent, override: true, require_option: false) ⇒ EcoRake::Options::Forwarding::Proxy Also known as: deep_dup

Returns with a copy of the rules.

Returns:



28
29
30
31
32
# File 'lib/eco-rake/options/forwarding/proxy.rb', line 28

def dup(parent, override: true, require_option: false)
  self.class.new(parent).tap do |rely|
    each {|rule| rely.add_rule(rule.deep_dup, override: override, require_option: require_option)}
  end
end

#each(&block) ⇒ Object



22
23
24
25
# File 'lib/eco-rake/options/forwarding/proxy.rb', line 22

def each(&block)
  return to_enum(:each) unless block_given?
  @rules.values.each(&block)
end

#forward(sym, from:) ⇒ Value

It applies the rule associated with sym to from parsed option results.

Parameters:

  • sym (Symbol)

    the short or name of an existing option.

  • from (Hash)

    the parsed option results.

Returns:

  • (Value)

    the value that the rule returns.



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

def forward(sym, from:)
  self[sym]&.apply(from)
end