Class: Harbor::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/harbor/generator.rb,
lib/harbor/generator/help.rb,
lib/harbor/generator/setup.rb

Defined Under Namespace

Classes: GeneratorArgumentError, GeneratorError, HelpCommand, SetupCommand, UnknownCommandError

Constant Summary collapse

@@generators =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, command, klass, description, help) ⇒ Generator

Returns a new instance of Generator.



14
15
16
# File 'lib/harbor/generator.rb', line 14

def initialize(app, command, klass, description, help)
  @app, @command, @klass, @description, @help = app, command, klass, description, help
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



12
13
14
# File 'lib/harbor/generator.rb', line 12

def app
  @app
end

#commandObject

Returns the value of attribute command.



12
13
14
# File 'lib/harbor/generator.rb', line 12

def command
  @command
end

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'lib/harbor/generator.rb', line 12

def description
  @description
end

#helpObject

Returns the value of attribute help.



12
13
14
# File 'lib/harbor/generator.rb', line 12

def help
  @help
end

#klassObject

Returns the value of attribute klass.



12
13
14
# File 'lib/harbor/generator.rb', line 12

def klass
  @klass
end

Class Method Details

.generatorsObject



48
49
50
# File 'lib/harbor/generator.rb', line 48

def self.generators
  @@generators
end

.register(app, command, klass, description = "", help = "") ⇒ Object



44
45
46
# File 'lib/harbor/generator.rb', line 44

def self.register(app, command, klass, description = "", help = "")
  @@generators["#{app}:#{command}"] = new(app, command, klass, description, help)
end

.run(app, command, options = []) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/harbor/generator.rb', line 18

def self.run(app, command, options = [])
  executor = if generator = @@generators["#{app}:#{command}"]
    generator.klass.new(options)
  else
    puts usage(app)

    exit(1)
  end

  executor.run
end

.usage(app) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/harbor/generator.rb', line 30

def self.usage(app)
  usage = []
  usage << "Usage #{app} command [options]"
  usage << ""
  usage << "Available commands:"

  @@generators.select { |key,| key =~ /^#{app}/ }.sort.each do |key, generator|
    usage << "%12s   %s" % [generator.command, generator.description]
  end

  usage
end