Class: Apimaster::Generators::Scripts::Base

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

Attributes included from Options

#options

Instance Method Summary collapse

Methods included from Options

included

Constructor Details

#initializeBase

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

#commandsObject

Returns the value of attribute commands.



26
27
28
# File 'lib/apimaster/generators/scripts.rb', line 26

def commands
  @commands
end

#stdoutObject (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 = [], runtime_options = {})
  @stdout = runtime_options[:stdout] || $stdout
  begin
    parse!(args.dup, runtime_options)
  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 options[:backtrace]
    raise SystemExit unless options[:no_exit]
  end
end