Class: KISSGen::Generator
- Inherits:
-
Object
- Object
- KISSGen::Generator
- Defined in:
- lib/kissgen/generator.rb
Overview
Instance Attribute Summary collapse
-
#assigns ⇒ Object
readonly
Returns the value of attribute assigns.
-
#copy_path ⇒ Object
readonly
Returns the value of attribute copy_path.
-
#explain_created ⇒ Object
Returns the value of attribute explain_created.
-
#explain_footer ⇒ Object
Returns the value of attribute explain_footer.
-
#explain_pretend ⇒ Object
Returns the value of attribute explain_pretend.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#setup_file_path ⇒ Object
Returns the value of attribute setup_file_path.
Instance Method Summary collapse
-
#assign(name, value) ⇒ Object
assigns :app_file_name, “lancelot”.
- #delete ⇒ Object
-
#directory(directory_path, relative_copy_path = directory_path, options = {}) ⇒ Object
Recursively adds a directory to the list of files to generate.
-
#file(relative_file_path, relative_copy_path = relative_file_path, options = {}) ⇒ Object
Adds a file to the list of files to generate.
- #generate(options = {}) ⇒ Object
-
#import_setup ⇒ Object
Import setup file which is located in /path/to/generator/setup.rb.
-
#initialize(path, copy_path) ⇒ Generator
constructor
A new instance of Generator.
Constructor Details
#initialize(path, copy_path) ⇒ Generator
Returns a new instance of Generator.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/kissgen/generator.rb', line 16 def initialize(path, copy_path) @path = path @copy_path = copy_path @files = [] @assigns = {} @explain_pretend = "== PRETEND MODE: Would have created the following files: ==" @explain_generate = "== Generated the following files: ==" @explain_footer = "====== Generator Finished ======" import_setup end |
Instance Attribute Details
#assigns ⇒ Object (readonly)
Returns the value of attribute assigns.
12 13 14 |
# File 'lib/kissgen/generator.rb', line 12 def assigns @assigns end |
#copy_path ⇒ Object (readonly)
Returns the value of attribute copy_path.
12 13 14 |
# File 'lib/kissgen/generator.rb', line 12 def copy_path @copy_path end |
#explain_created ⇒ Object
Returns the value of attribute explain_created.
13 14 15 |
# File 'lib/kissgen/generator.rb', line 13 def explain_created @explain_created end |
#explain_footer ⇒ Object
Returns the value of attribute explain_footer.
13 14 15 |
# File 'lib/kissgen/generator.rb', line 13 def @explain_footer end |
#explain_pretend ⇒ Object
Returns the value of attribute explain_pretend.
13 14 15 |
# File 'lib/kissgen/generator.rb', line 13 def explain_pretend @explain_pretend end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
12 13 14 |
# File 'lib/kissgen/generator.rb', line 12 def files @files end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/kissgen/generator.rb', line 12 def path @path end |
#setup_file_path ⇒ Object
Returns the value of attribute setup_file_path.
14 15 16 |
# File 'lib/kissgen/generator.rb', line 14 def setup_file_path @setup_file_path end |
Instance Method Details
#assign(name, value) ⇒ Object
assigns :app_file_name, “lancelot”
77 78 79 |
# File 'lib/kissgen/generator.rb', line 77 def assign(name, value) @assigns[name] = value end |
#delete ⇒ Object
81 82 83 |
# File 'lib/kissgen/generator.rb', line 81 def delete @files.each { |file| file.delete } end |
#directory(directory_path, relative_copy_path = directory_path, options = {}) ⇒ Object
Recursively adds a directory to the list of files to generate
directory “app”
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/kissgen/generator.rb', line 65 def directory(directory_path, relative_copy_path = directory_path, = {}) Dir["#{File.join(@path, directory_path)}/**/*"].each do |file_path| unless FileTest.directory?(file_path) # Take the template file path and give relative paths relative_path = Pathname.new(file_path).relative_path_from(Pathname.new(File.join(@path, directory_path))) new_path = File.join(relative_copy_path, relative_path) @files << Template.new(self, File.join(directory_path, relative_path), new_path) end end end |
#file(relative_file_path, relative_copy_path = relative_file_path, options = {}) ⇒ Object
Adds a file to the list of files to generate
This will generate the template README file relative to the copy path
file "README"
or
file "README", "README_PLEASE"
56 57 58 59 60 |
# File 'lib/kissgen/generator.rb', line 56 def file(relative_file_path, relative_copy_path = relative_file_path, = {}) template = Template.new(self, relative_file_path, relative_copy_path) @files << template template end |
#generate(options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/kissgen/generator.rb', line 37 def generate( = {}) puts([:pretend] ? @explain_pretend : @explain_generate) @files.each do |file| [:pretend] ? puts("#{file.copy_path}") : file.create end puts @explain_footer end |
#import_setup ⇒ Object
Import setup file which is located in /path/to/generator/setup.rb
33 34 35 |
# File 'lib/kissgen/generator.rb', line 33 def import_setup instance_eval(File.new(setup_file_path).read) if File.exists?(setup_file_path) end |