Class: Ing::Dispatcher
- Inherits:
-
Object
- Object
- Ing::Dispatcher
- 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().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, )
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#dispatch_class ⇒ Object
Returns the value of attribute dispatch_class.
-
#dispatch_meth ⇒ Object
Returns the value of attribute dispatch_meth.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.dispatched ⇒ Object
Global set of dispatched commands as [dispatch_class, dispatch_meth], updated before dispatch.
-
.execute(klass, *args, &config) ⇒ Object
Ing.execute
. -
.invoke(klass, *args, &config) ⇒ Object
Ing.invoke
.
Instance Method Summary collapse
-
#describe ⇒ Object
Returns stream (StringIO) of description text from specify_options.
-
#dispatch(&config) ⇒ Object
Public dispatch method used by all types of dispatch (run, invoke, execute).
-
#dispatched? ⇒ Boolean
True if current dispatch class/method has been dispatched before.
-
#help ⇒ Object
Returns stream (StringIO) of help text from specify_options.
-
#initialize(namespaces, classes, *args) ⇒ Dispatcher
constructor
Default constructor from
Ing.run
(command line). -
#initialize_preloaded(invoking, klass, *args) ⇒ Object
Alternate constructor for preloaded object and arguments i.e.
-
#with_option_parser(klass) {|p| ... } ⇒ Object
:nodoc:.
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
#args ⇒ Object
Returns the value of attribute args.
37 38 39 |
# File 'lib/ing/dispatcher.rb', line 37 def args @args end |
#dispatch_class ⇒ Object
Returns the value of attribute dispatch_class.
37 38 39 |
# File 'lib/ing/dispatcher.rb', line 37 def dispatch_class @dispatch_class end |
#dispatch_meth ⇒ Object
Returns the value of attribute dispatch_meth.
37 38 39 |
# File 'lib/ing/dispatcher.rb', line 37 def dispatch_meth @dispatch_meth end |
#options ⇒ Object
Returns the value of attribute options.
37 38 39 |
# File 'lib/ing/dispatcher.rb', line 37 def @options end |
Class Method Details
.dispatched ⇒ Object
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
#describe ⇒ Object
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. 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
40 41 42 |
# File 'lib/ing/dispatcher.rb', line 40 def dispatched? Dispatcher.dispatched.include?([dispatch_class,dispatch_meth]) end |
#help ⇒ Object
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. = (Hash === args.last ? args.pop : {}) self.dispatch_class = klass self.dispatch_meth = extract_method!(args, dispatch_class) self.args = args @invoking = invoking end |