Class: Tap::App::Api

Inherits:
Object
  • Object
show all
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, Task

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Signals

#sig, #signal, #signal?, #signals

Methods included from Signals::ModuleMethods

included

Constructor Details

#initialize(config = {}, app = Tap::App.current) ⇒ Api

Returns a new instance of Api.



113
114
115
116
# File 'lib/tap/app/api.rb', line 113

def initialize(config={}, app=Tap::App.current)
  @app = app
  initialize_config(config)
end

Class Attribute Details

.typeObject (readonly)

The type of the class.



13
14
15
# File 'lib/tap/app/api.rb', line 13

def type
  @type
end

Instance Attribute Details

#appObject (readonly)

The app for self



111
112
113
# File 'lib/tap/app/api.rb', line 111

def app
  @app
end

Class Method Details

.build(spec = {}, app = Tap::App.current) ⇒ Object

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



81
82
83
# File 'lib/tap/app/api.rb', line 81

def build(spec={}, app=Tap::App.current)
  new(spec['config'] || {}, app)
end

.helpObject

Returns a help string that formats the desc documentation.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tap/app/api.rb', line 86

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:



15
16
17
18
19
20
21
22
23
24
# File 'lib/tap/app/api.rb', line 15

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.current, &block) ⇒ Object



56
57
58
# File 'lib/tap/app/api.rb', line 56

def parse(argv=ARGV, app=Tap::App.current, &block)
  parse!(argv.dup, app, &block)
end

.parse!(argv = ARGV, app = Tap::App.current) ⇒ 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.

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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tap/app/api.rb', line 66

def parse!(argv=ARGV, app=Tap::App.current)
  parser = self.parser(app)
  args = parser.parse!(argv, :add_defaults => false)
  obj = build(convert_to_spec(parser, args), app)
  
  if block_given?
    yield(obj, args)
  else
    parser.warn_ignored_args(args)
    obj
  end
end

.parser(app) ⇒ 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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tap/app/api.rb', line 32

def parser(app)
  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("--help", "Print this help") do
    lines = ["#{self}#{desc.empty? ? '' : ' -- '}#{desc.to_s}"]
    lines << help
    lines << "usage: tap #{to_s.underscore} #{respond_to?(:args) ? args : nil}"
    lines << nil
    lines << opts
    raise lines.join("\n")
  end
  
  opts
end

Instance Method Details

#associationsObject

By default associations returns nil.



119
120
# File 'lib/tap/app/api.rb', line 119

def associations
end

#inspectObject

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



131
132
133
# File 'lib/tap/app/api.rb', line 131

def inspect
  "#<#{self.class.to_s}:#{object_id} #{config.to_hash.inspect} >"
end

#to_specObject

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



124
125
126
127
# File 'lib/tap/app/api.rb', line 124

def to_spec
  config = self.config.to_hash {|hash, key, value| hash[key.to_s] = value }
  config.empty? ? {} : {'config' => config}
end