Class Tap::App::Api
In: lib/tap/app/api.rb
Parent: Object

Api implements the application interface described in the API document, and provides additional functionality shared by the Tap base classes.

Methods

associations   build   convert_to_spec   help   inspect   new   parse   parse!   parser   to_spec  

Included Modules

Configurable Signals

Attributes

app  [R]  The app for self
type  [R]  The type of the class.

Public Class methods

Returns an instance of self. By default build calls new with the configurations specified by spec[‘config’], and app.

[Source]

    # File lib/tap/app/api.rb, line 81
81:         def build(spec={}, app=Tap::App.current)
82:           new(spec['config'] || {}, app)
83:         end

Returns a help string that formats the desc documentation.

[Source]

    # File lib/tap/app/api.rb, line 86
86:         def help
87:           lines = desc.kind_of?(Lazydoc::Comment) ? desc.wrap(77, 2, nil) : []
88:           lines.collect! {|line| "  #{line}"}
89:           unless lines.empty?
90:             line = '-' * 80
91:             lines.unshift(line)
92:             lines.push(line)
93:           end
94: 
95:           lines.join("\n")
96:         end

[Source]

     # File lib/tap/app/api.rb, line 113
113:       def initialize(config={}, app=Tap::App.current)
114:         @app = app
115:         initialize_config(config)
116:       end

[Source]

    # File lib/tap/app/api.rb, line 56
56:         def parse(argv=ARGV, app=Tap::App.current, &block)
57:           parse!(argv.dup, app, &block)
58:         end

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.

Returns the instance. If a block is given, the instance and any remaining arguments will be yielded to it.

[Source]

    # File lib/tap/app/api.rb, line 66
66:         def parse!(argv=ARGV, app=Tap::App.current)
67:           parser = self.parser(app)
68:           args = parser.parse!(argv, :add_defaults => false)
69:           obj = build(convert_to_spec(parser, args), app)
70:           
71:           if block_given?
72:             yield(obj, args)
73:           else
74:             parser.warn_ignored_args(args)
75:             obj
76:           end
77:         end

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.

[Source]

    # File lib/tap/app/api.rb, line 32
32:         def parser(app)
33:           opts = ConfigParser.new
34:           
35:           unless configurations.empty?
36:             opts.separator "configurations:"
37:             opts.add(configurations)
38:             opts.separator ""
39:           end
40: 
41:           opts.separator "options:"
42:         
43:           # add option to print help
44:           opts.on("--help", "Print this help") do
45:             lines = ["#{self}#{desc.empty? ? '' : ' -- '}#{desc.to_s}"]
46:             lines << help
47:             lines << "usage: tap #{to_s.underscore} #{respond_to?(:args) ? args : nil}"
48:             lines << nil
49:             lines << opts
50:             raise lines.join("\n")
51:           end
52:           
53:           opts
54:         end

Protected Class methods

[Source]

     # File lib/tap/app/api.rb, line 100
100:         def convert_to_spec(parser, args)
101:           {'config' => parser.nested_config}
102:         end

Public Instance methods

By default associations returns nil.

[Source]

     # File lib/tap/app/api.rb, line 119
119:       def associations
120:       end

Provides an abbreviated version of the default inspect, with only the class, object_id, and configurations listed.

[Source]

     # File lib/tap/app/api.rb, line 131
131:       def inspect
132:         "#<#{self.class.to_s}:#{object_id} #{config.to_hash.inspect} >"
133:       end

By default to_spec returns a hash like {‘config’ => config} where config is a stringified representation of the configurations for self.

[Source]

     # File lib/tap/app/api.rb, line 124
124:       def to_spec
125:         config = self.config.to_hash {|hash, key, value| hash[key.to_s] = value }
126:         config.empty? ? {} : {'config' => config}
127:       end

[Validate]