Class: Merb::Generators::Generator

Inherits:
Templater::Generator
  • Object
show all
Includes:
ColorfulMessages
Defined in:
merb-gen/lib/merb-gen/generator.rb

Direct Known Subclasses

NamedGenerator

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from ColorfulMessages

#error, #success, #warning

Constructor Details

- (Generator) initialize(*args)

Returns a new instance of Generator



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'merb-gen/lib/merb-gen/generator.rb', line 32

def initialize(*args)
  Merb::Config.setup({
    :log_level        => :fatal,
    :log_delimiter    => " ~ ",
    :log_auto_flush   => false,
    :reload_templates => false,
    :reload_classes   => false
  })

  Merb::BootLoader::Logger.run
  Merb::BootLoader::BuildFramework.run
  Merb::BootLoader::Dependencies.run
  
  super
  options[:orm] ||= Merb.orm
  options[:testing_framework] ||= Merb.test_framework
  options[:template_engine] ||= Merb.template_engine
end

Class Method Details

+ (Object) source_root



81
82
83
# File 'merb-gen/lib/merb-gen/generator.rb', line 81

def self.source_root
  File.join(File.dirname(__FILE__), '..', 'generators', 'templates')
end

Instance Method Details

- (String) go_up(num)

Returns a string of num times '..', useful for example in tests for namespaced generators to find the spec_helper higher up in the directory structure.

Parameters:

  • num (Integer)

    Number of directories up.

Returns:

  • (String)

    Concatenated string.



77
78
79
# File 'merb-gen/lib/merb-gen/generator.rb', line 77

def go_up(num)
  (["'..'"] * num).join(', ')
end

- (Object) with_modules(modules, options = {}, &block)

Inside a template, wraps a block of code properly in modules, keeping the indentation correct.

Parameters:

  • modules (Array<#to_s>)

    Array of modules to use for nesting.

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

    Additional options.

Options Hash (options):

  • indent (Integer) — default: 0

    Number of levels to indent the modules.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'merb-gen/lib/merb-gen/generator.rb', line 58

def with_modules(modules, options={}, &block)
  indent = options[:indent] || 0
  text = capture(&block)
  modules.each_with_index do |mod, i|
    concat(("  " * (indent + i)) + "module #{mod}\n", block.binding)
  end
  text = Array(text).map{ |line| ("  " * modules.size) + line }.join
  concat(text, block.binding)
  modules.reverse.each_with_index do |mod, i|
    concat(("  " * (indent + modules.size - i - 1)) + "end # #{mod}\n", block.binding)
  end
end