Class: Generator
- Inherits:
-
Object
- Object
- Generator
- Defined in:
- lib/generator/generator.rb
Instance Attribute Summary collapse
-
#templates_path ⇒ String
readonly
Return template folder path.
Class Method Summary collapse
- .generator ⇒ Object
- .inherited(module_class) ⇒ Object
- .remove(group, options = {}) ⇒ Object
- .run(group, options = {}) ⇒ Object
- .templates_path ⇒ Object
- .templates_path=(path) ⇒ Object
Instance Method Summary collapse
-
#create_file(path, template_name, file_name = nil) ⇒ Object
Create new file from template.
-
#create_folder(folder, force = false) ⇒ Object
Create new folder.
-
#initialize(options = {}) ⇒ Generator
constructor
A new instance of Generator.
- #remove_file(path) ⇒ Object
-
#remove_folder(folder) ⇒ Object
Remove folder.
Constructor Details
#initialize(options = {}) ⇒ Generator
Returns a new instance of Generator.
15 16 17 18 |
# File 'lib/generator/generator.rb', line 15 def initialize( = {}) @templates_path = [:templates_path] || Generator.templates_path @options = end |
Instance Attribute Details
#templates_path ⇒ String (readonly)
Return template folder path.
5 6 7 |
# File 'lib/generator/generator.rb', line 5 def templates_path @templates_path end |
Class Method Details
.generator ⇒ Object
7 8 9 |
# File 'lib/generator/generator.rb', line 7 def self.generator @generator ||= [] end |
.inherited(module_class) ⇒ Object
11 12 13 |
# File 'lib/generator/generator.rb', line 11 def self.inherited(module_class) self.generator << module_class end |
.remove(group, options = {}) ⇒ Object
24 25 26 |
# File 'lib/generator/generator.rb', line 24 def self.remove(group, = {}) self.generator.each { |generator| generator.new().remove if generator.group == group } end |
.run(group, options = {}) ⇒ Object
20 21 22 |
# File 'lib/generator/generator.rb', line 20 def self.run(group, = {}) self.generator.each { |generator| generator.new().run if generator.group == group } end |
.templates_path ⇒ Object
28 29 30 |
# File 'lib/generator/generator.rb', line 28 def self.templates_path @templates_path end |
.templates_path=(path) ⇒ Object
32 33 34 |
# File 'lib/generator/generator.rb', line 32 def self.templates_path= path @templates_path = path || File.join(Dir.pwd, 'lib/generators/templates') end |
Instance Method Details
#create_file(path, template_name, file_name = nil) ⇒ Object
Create new file from template
54 55 56 57 58 59 60 |
# File 'lib/generator/generator.rb', line 54 def create_file(path, template_name, file_name = nil) template = File.join @templates_path, template_name template_file = File.open(template, 'r') { |file| ERB.new(file.read).result binding } file_name = File.basename template_name, '.*' if file_name.nil? file_name = File.join( path, file_name) File.open(file_name, 'w') { |file| file.write template_file } end |
#create_folder(folder, force = false) ⇒ Object
Create new folder
39 40 41 42 |
# File 'lib/generator/generator.rb', line 39 def create_folder(folder, force = false) FileUtils.rm_rf folder if force FileUtils.makedirs folder unless Dir.exist? folder end |
#remove_file(path) ⇒ Object
62 63 64 |
# File 'lib/generator/generator.rb', line 62 def remove_file(path) FileUtils.rm_rf path if File.exist? path end |
#remove_folder(folder) ⇒ Object
Remove folder
46 47 48 |
# File 'lib/generator/generator.rb', line 46 def remove_folder(folder) FileUtils.rm_rf folder if Dir.exist? folder end |