Method: Merb::Generators::Generator#with_modules

Defined in:
lib/merb-gen/generator.rb

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

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

Parameters:

  • modules (Array[#to_s])

    an array of modules to use for nesting

  • indent<Integer> (Hash)

    a customizable set of options



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/merb-gen/generator.rb', line 55

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