Class: Lugg::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/lugg/switch.rb

Overview

An object representing a command line swith with certain behaviour. The Runner applies switches to its own internal OptionParser object to build the CLI and provide querying functionality.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method = :on) ⇒ Switch

Returns a new instance of Switch.



24
25
26
27
28
29
30
# File 'lib/lugg/switch.rb', line 24

def initialize(method = :on)
  @method = method
  @flags  = nil
  @cast   = nil
  @desc   = nil
  @action = nil
end

Class Method Details

.apply_all(*args) ⇒ Object

Call #apply on each Lugg::Switch in the global collection to register the callback on a given object.



18
19
20
21
22
# File 'lib/lugg/switch.rb', line 18

def self.apply_all(*args)
  @queries.each do |query|
    query.apply_to(*args)
  end
end

.define(*args, &block) ⇒ Object

Define a new Switch object and add it to the global collection of switches.



12
13
14
# File 'lib/lugg/switch.rb', line 12

def self.define(*args, &block)
  @queries << new(*args).tap { |obj| obj.instance_eval(&block) }
end

Instance Method Details

#apply_to(options, obj) ⇒ Object



32
33
34
35
36
# File 'lib/lugg/switch.rb', line 32

def apply_to(options, obj)
  @options = options
  @obj     = obj
  @options.send(@method, *[@flags, @cast, @desc].flatten.compact, &@action)
end