Class: Apimaster::Generators::Scripts::Base
- Inherits:
-
Object
- Object
- Apimaster::Generators::Scripts::Base
- Includes:
- Options
- Defined in:
- lib/apimaster/generators/scripts.rb
Overview
Generator scripts handle command-line invocation. Each script responds to an invoke! class method which handles option parsing and generator invocation.
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Attributes included from Options
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #register(name, klass) ⇒ Object
-
#run(args = [], runtime_options = {}) ⇒ Object
Run the generator script.
Methods included from Options
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
28 29 30 31 32 33 |
# File 'lib/apimaster/generators/scripts.rb', line 28 def initialize @commands ||= {} register("new", AppGenerator) register("model", ModelGenerator) register("controller", ControllerGenerator) end |
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
26 27 28 |
# File 'lib/apimaster/generators/scripts.rb', line 26 def commands @commands end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
25 26 27 |
# File 'lib/apimaster/generators/scripts.rb', line 25 def stdout @stdout end |
Instance Method Details
#register(name, klass) ⇒ Object
35 36 37 |
# File 'lib/apimaster/generators/scripts.rb', line 35 def register name, klass @commands[name] = klass end |
#run(args = [], runtime_options = {}) ⇒ Object
Run the generator script. Takes an array of unparsed arguments and a hash of parsed arguments, takes the generator as an option or first remaining argument, and invokes the requested command.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/apimaster/generators/scripts.rb', line 42 def run(args = [], = {}) @stdout = [:stdout] || $stdout begin parse!(args.dup, ) rescue OptionParser::InvalidOption => e # Don't cry, script. Generators want what you think is invalid. end # Look up generator instance and invoke command on it. begin command = args.shift if command and commands.key?(command) commands[command].new(args).run else raise "Invalid command name: #{command}" end rescue => e stdout.puts e stdout.puts " #{e.backtrace.join("\n ")}\n" if [:backtrace] raise SystemExit unless [:no_exit] end end |