Class: Mack::Generator::Base
- Inherits:
-
Object
- Object
- Mack::Generator::Base
- Includes:
- FileUtils
- Defined in:
- lib/generators/generator_base.rb
Overview
All generator classes should extend this class if they’re expected to be used by the rake generate:<generator_name> task. A generator must by name in the following style: <name>Generator.
Example:
class MyCoolGenerator < Mack::Generator::Base
require_param :name, :foo
def generate
# do work...
end
end
rake generate:my_cool # => calls MyCoolGenerator
Direct Known Subclasses
Class Method Summary collapse
-
.require_param(*args) ⇒ Object
Used to define arguments that are required by the generator.
-
.required_params ⇒ Object
Returns the required_params array.
Instance Method Summary collapse
-
#directory(output_dir, options = {}) ⇒ Object
Creates the specified directory.
-
#initialize(env = {}) ⇒ Base
constructor
Takes a Hash of parameters.
-
#param(key) ⇒ Object
Returns a parameter from the initial Hash of parameters.
-
#run ⇒ Object
Runs the generate method.
-
#template(input_file, output_file, options = {}) ⇒ Object
Takes an input_file runs it through ERB and saves it to the specified output_file.
Constructor Details
#initialize(env = {}) ⇒ Base
Takes a Hash of parameters. Raise Mack::Errors::RequiredGeneratorParameterMissing if a required parameter is missing.
34 35 36 37 38 39 |
# File 'lib/generators/generator_base.rb', line 34 def initialize(env = {}) @env = env self.class.required_params.each do |p| raise Mack::Errors::RequiredGeneratorParameterMissing.new(p) unless param(p) end end |
Class Method Details
.require_param(*args) ⇒ Object
Used to define arguments that are required by the generator.
21 22 23 24 |
# File 'lib/generators/generator_base.rb', line 21 def self.require_param(*args) required_params << args required_params.flatten! end |
.required_params ⇒ Object
Returns the required_params array.
27 28 29 |
# File 'lib/generators/generator_base.rb', line 27 def self.required_params @required_params ||= [] end |
Instance Method Details
#directory(output_dir, options = {}) ⇒ Object
Creates the specified directory.
70 71 72 73 74 75 76 77 |
# File 'lib/generators/generator_base.rb', line 70 def directory(output_dir, = {}) if File.exists?(output_dir) puts "Exists: #{output_dir}" return end mkdir_p(output_dir) puts "Created: #{output_dir}" end |
#param(key) ⇒ Object
Returns a parameter from the initial Hash of parameters.
47 48 49 |
# File 'lib/generators/generator_base.rb', line 47 def param(key) (@env[key.to_s.downcase] ||= @env[key.to_s.upcase]) end |
#run ⇒ Object
Runs the generate method.
42 43 44 |
# File 'lib/generators/generator_base.rb', line 42 def run generate end |
#template(input_file, output_file, options = {}) ⇒ Object
Takes an input_file runs it through ERB and saves it to the specified output_file. If the output_file exists it will be skipped. If you would like to force the writing of the file, use the :force => true option.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/generators/generator_base.rb', line 58 def template(input_file, output_file, = {}) if File.exists?(output_file) unless [:force] puts "Skipped: #{output_file}" return end end File.open(output_file, "w") {|f| f.puts ERB.new(File.open(input_file).read).result(binding)} puts "Wrote: #{output_file}" end |