Class: Ing::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ing/dispatcher.rb

Overview

Generic router for ing commands (both built-in and user-defined). Resolves class, parses options with Trollop if target class defines specify_options, then dispatches like (simplifying):

Target.new(options).send(*args)

if the target is class-like, i.e. responds to new. Otherwise it dispatches to the target as a callable:

Target.call(*args, options)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespaces, classes, *args) ⇒ Dispatcher

Default constructor from Ing.run (command line)



45
46
47
48
49
50
51
# File 'lib/ing/dispatcher.rb', line 45

def initialize(namespaces, classes, *args)
  ns                  = Util.namespaced_const_get(namespaces)
  self.dispatch_class = Util.namespaced_const_get(classes, ns)
  self.dispatch_meth  = extract_method!(args, dispatch_class)
  self.args           = args
  @invoking           = false
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



37
38
39
# File 'lib/ing/dispatcher.rb', line 37

def args
  @args
end

#dispatch_classObject

Returns the value of attribute dispatch_class.



37
38
39
# File 'lib/ing/dispatcher.rb', line 37

def dispatch_class
  @dispatch_class
end

#dispatch_methObject

Returns the value of attribute dispatch_meth.



37
38
39
# File 'lib/ing/dispatcher.rb', line 37

def dispatch_meth
  @dispatch_meth
end

#optionsObject

Returns the value of attribute options.



37
38
39
# File 'lib/ing/dispatcher.rb', line 37

def options
  @options
end

Class Method Details

.dispatchedObject

Global set of dispatched commands as [dispatch_class, dispatch_meth], updated before dispatch



21
22
23
# File 'lib/ing/dispatcher.rb', line 21

def self.dispatched
  @dispatched ||= Set.new
end

.execute(klass, *args, &config) ⇒ Object

Ing.execute



32
33
34
35
# File 'lib/ing/dispatcher.rb', line 32

def self.execute(klass, *args, &config)
  allocate.tap {|d| d.initialize_preloaded(false, klass, *args) }.
    dispatch(&config)
end

.invoke(klass, *args, &config) ⇒ Object

Ing.invoke



26
27
28
29
# File 'lib/ing/dispatcher.rb', line 26

def self.invoke(klass, *args, &config)
  allocate.tap {|d| d.initialize_preloaded(true, klass, *args) }.
    dispatch(&config)
end

Instance Method Details

#describeObject

Returns stream (StringIO) of description text from specify_options. Note this does not parse the options. Used by Ing::Commands::List.



65
66
67
68
69
70
71
# File 'lib/ing/dispatcher.rb', line 65

def describe
  s=StringIO.new
  with_option_parser(self.dispatch_class) do |p|
    p.educate_banner s
  end
  s.rewind; s
end

#dispatch(&config) ⇒ Object

Public dispatch method used by all types of dispatch (run, invoke, execute). Does not dispatch if invoking and already dispatched.



85
86
87
88
89
90
# File 'lib/ing/dispatcher.rb', line 85

def dispatch(&config)
  unless @invoking && dispatched?
    record_dispatch
    execute(&config)
  end
end

#dispatched?Boolean

True if current dispatch class/method has been dispatched before

Returns:

  • (Boolean)


40
41
42
# File 'lib/ing/dispatcher.rb', line 40

def dispatched?
  Dispatcher.dispatched.include?([dispatch_class,dispatch_meth])
end

#helpObject

Returns stream (StringIO) of help text from specify_options. Note this does not parse the options. Used by Ing::Commands::Help.



75
76
77
78
79
80
81
# File 'lib/ing/dispatcher.rb', line 75

def help
  s=StringIO.new
  with_option_parser(self.dispatch_class) do |p|
    p.educate s
  end
  s.rewind; s   
end

#initialize_preloaded(invoking, klass, *args) ⇒ Object

Alternate constructor for preloaded object and arguments i.e. from invoke or execute instead of run



55
56
57
58
59
60
61
# File 'lib/ing/dispatcher.rb', line 55

def initialize_preloaded(invoking, klass, *args)
  self.options        = (Hash === args.last ? args.pop : {})
  self.dispatch_class = klass
  self.dispatch_meth  = extract_method!(args, dispatch_class)
  self.args           = args
  @invoking           = invoking
end

#with_option_parser(klass) {|p| ... } ⇒ Object

:nodoc:

Yields:

  • (p)


92
93
94
95
96
# File 'lib/ing/dispatcher.rb', line 92

def with_option_parser(klass)   # :nodoc:
  return unless klass.respond_to?(:specify_options)
  klass.specify_options(p = Trollop::Parser.new)
  yield p
end