Class: Tap::App::Api
- Inherits:
-
Object
- Object
- Tap::App::Api
- Includes:
- Configurable, Signals
- Defined in:
- lib/tap/app/api.rb
Overview
Api implements the application interface described in the API document, and provides additional functionality shared by the Tap base classes.
Direct Known Subclasses
Join, Middleware, Prompt, Task
Class Attribute Summary collapse
-
.type ⇒ Object
readonly
The type of the class.
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
The App receiving self during enq.
Class Method Summary collapse
-
.build(spec = {}, app = Tap::App.instance) ⇒ Object
Returns an instance of self.
-
.help ⇒ Object
Returns a help string that formats the desc documentation.
-
.inherited(child) ⇒ Object
:nodoc:.
-
.parse(argv = ARGV, app = Tap::App.instance) ⇒ Object
Parses the argv into an instance of self.
-
.parse!(argv = ARGV, app = Tap::App.instance) ⇒ Object
Same as parse, but removes arguments destructively.
-
.parser ⇒ Object
Returns a ConfigParser setup to parse the configurations for the subclass.
Instance Method Summary collapse
-
#associations ⇒ Object
By default associations returns nil.
-
#initialize(config = {}, app = Tap::App.instance) ⇒ Api
constructor
A new instance of Api.
-
#to_spec ⇒ Object
By default to_spec returns a hash like => config where config is a stringified representation of the configurations for self.
Methods included from Signals
Methods included from Signals::ModuleMethods
Constructor Details
#initialize(config = {}, app = Tap::App.instance) ⇒ Api
Returns a new instance of Api.
98 99 100 101 |
# File 'lib/tap/app/api.rb', line 98 def initialize(config={}, app=Tap::App.instance) @app = app initialize_config(config) end |
Class Attribute Details
.type ⇒ Object (readonly)
The type of the class.
14 15 16 |
# File 'lib/tap/app/api.rb', line 14 def type @type end |
Instance Attribute Details
#app ⇒ Object (readonly)
The App receiving self during enq
96 97 98 |
# File 'lib/tap/app/api.rb', line 96 def app @app end |
Class Method Details
.build(spec = {}, app = Tap::App.instance) ⇒ Object
Returns an instance of self. By default build calls new with the configurations specified by spec, and app.
72 73 74 |
# File 'lib/tap/app/api.rb', line 72 def build(spec={}, app=Tap::App.instance) new(spec['config'] || {}, app) end |
.help ⇒ Object
Returns a help string that formats the desc documentation.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/tap/app/api.rb', line 77 def help lines = desc.kind_of?(Lazydoc::Comment) ? desc.wrap(77, 2, nil) : [] lines.collect! {|line| " #{line}"} unless lines.empty? line = '-' * 80 lines.unshift(line) lines.push(line) end lines.join("\n") end |
.inherited(child) ⇒ Object
:nodoc:
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tap/app/api.rb', line 16 def inherited(child) # :nodoc: super type = self.type || child.to_s.split('::').last.downcase child.instance_variable_set(:@type, type) unless child.respond_to?(:desc) child.lazy_attr(:desc, type) end end |
.parse(argv = ARGV, app = Tap::App.instance) ⇒ Object
Parses the argv into an instance of self. Internally parse parses an argh then calls build, but there is no requirement that this occurs in subclasses.
58 59 60 |
# File 'lib/tap/app/api.rb', line 58 def parse(argv=ARGV, app=Tap::App.instance) parse!(argv.dup, app) end |
.parse!(argv = ARGV, app = Tap::App.instance) ⇒ Object
Same as parse, but removes arguments destructively.
63 64 65 66 67 68 |
# File 'lib/tap/app/api.rb', line 63 def parse!(argv=ARGV, app=Tap::App.instance) parser = self.parser argv = parser.parse!(argv, :add_defaults => false) [build({'config' => parser.nested_config}, app), argv] end |
.parser ⇒ Object
Returns a ConfigParser setup to parse the configurations for the subclass. The parser is also setup to print usage (using the desc for the subclass) and exit for the ‘-h’ and ‘–help’ options.
The parse method uses parser by default, so subclasses can simply modify parser and ensure parse still works correctly.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/tap/app/api.rb', line 33 def parser opts = ConfigParser.new unless configurations.empty? opts.separator "configurations:" opts.add(configurations) opts.separator "" end opts.separator "options:" # add option to print help opts.on("-h", "--help", "Print this help") do puts "#{self}#{desc.empty? ? '' : ' -- '}#{desc.to_s}" puts help puts opts exit end opts end |
Instance Method Details
#associations ⇒ Object
By default associations returns nil.
104 105 |
# File 'lib/tap/app/api.rb', line 104 def associations end |
#to_spec ⇒ Object
By default to_spec returns a hash like => config where config is a stringified representation of the configurations for self.
109 110 111 112 |
# File 'lib/tap/app/api.rb', line 109 def to_spec config = self.config.to_hash {|hash, key, value| hash[key.to_s] = value } {'config' => config} end |