Class: Lightning::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/lightning/generator.rb

Overview

Generates globs for bolts using methods defined in Generators. Generated bolts are inserted under Lightning.config. Users can define their own generators with generator plugins.

Constant Summary collapse

DEFAULT_GENERATORS =
%w{gem ruby local_ruby wild}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



39
40
41
# File 'lib/lightning/generator.rb', line 39

def initialize
  @underling = Object.new.extend(Generators)
end

Instance Attribute Details

#underlingObject (readonly)

Object used to call generator(s)



38
39
40
# File 'lib/lightning/generator.rb', line 38

def underling
  @underling
end

Class Method Details

.generatorsHash

Returns Maps generators to their descriptions.

Returns:

  • (Hash)

    Maps generators to their descriptions



9
10
11
12
# File 'lib/lightning/generator.rb', line 9

def self.generators
  load_plugins
  Generators.generators
end

.load_pluginsObject

Loads default and user generator plugins



29
30
31
32
33
34
35
# File 'lib/lightning/generator.rb', line 29

def self.load_plugins
  @loaded ||= begin
    Util.load_plugins File.dirname(__FILE__), 'generators'
    Util.load_plugins Lightning.dir, 'generators'
    true
  end
end

.run(gens = [], options = {}) ⇒ Object

Runs generators

Parameters:

  • Generators (Array<String>)

    instance methods

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :once (String)

    Generator to run once

  • :test (Boolean)

    Runs generators in test mode which only displays generated globs and doesn’t save them

  • :live (Boolean)

    Generates globs for use at runtime



21
22
23
24
25
26
# File 'lib/lightning/generator.rb', line 21

def self.run(gens=[], options={})
  load_plugins
  new.run(gens, options)
rescue
  $stderr.puts "Error: #{$!.message}"
end

Instance Method Details

#run(gens, options) ⇒ nil, true

Returns Main method which runs generators.

Returns:

  • (nil, true)

    Main method which runs generators



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lightning/generator.rb', line 44

def run(gens, options)
  if options.key?(:once)
    run_once(gens, options)
  elsif options.key?(:live)
    Array(call_generator(gens, false))
  else
    gens = DEFAULT_GENERATORS if Array(gens).empty?
    gens = Hash[*gens.zip(gens).flatten] if gens.is_a?(Array)
    generate_bolts gens
  end
end