Class: Ptero::Generator
- Inherits:
-
Object
- Object
- Ptero::Generator
- Defined in:
- lib/ptero/generator.rb
Overview
An object that generates files for an application
Direct Known Subclasses
GitignoreGenerator, HTAccessGenerator, JavascriptGenerator, PHPGenerator, StylesheetGenerator, TwigGenerator
Defined Under Namespace
Classes: ApplicationControllerGenerator, ApplicationJavascriptGenerator, ApplicationStylesheetGenerator, ConfigGenerator, ControllerGenerator, GitignoreGenerator, HTAccessGenerator, JavascriptGenerator, LandingGenerator, LayoutGenerator, LoadAllGenerator, ModelGenerator, PHPClassGenerator, PHPGenerator, PHPInfoGenerator, PageGenerator, PageNotFoundGenerator, RoutesGenerator, SetupGenerator, StylesheetGenerator, TwigGenerator, ViewGenerator
Constant Summary collapse
- TEMPLATE_PATH =
The directory location of all Ptero templates for code generation
Pathname.new("#{__dir__}/templates")
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.const_missing(const_name) ⇒ Object
Autoload Generators.
Instance Method Summary collapse
-
#content ⇒ String
Return the content of the file to be generated by inputting content_params into an erubis template.
-
#content_params ⇒ Generator
The context for generating a template, default to self.
-
#extension ⇒ String
The extension of the file to be generated.
-
#filename ⇒ String
The filename of the generated file.
-
#generate ⇒ Generator
Generate a file and print the location of the generated file.
-
#generated? ⇒ Boolean
Find out if this Generator’s file is generated.
-
#initialize(name, app = Application.app_for(Dir.pwd)) ⇒ Generator
constructor
Input the generator’s name and the Application to generate for.
-
#location ⇒ String
The fully-qualified filename of the file to be generated by this object.
-
#path ⇒ String
Default path to write to, used along with filename to determine the destination of the generated file.
-
#reload ⇒ Generator
Remove and regenerate and print the regenerated file.
-
#remove ⇒ Generator
Remove the file corresponding to self and print its location.
-
#template_path ⇒ String
The path to the directory where Ptero templates are stored.
-
#to_s ⇒ String
Simple string representation of this object, represented by the unqualified class name and filename of the current object.
-
#type ⇒ String
The unqualified name of the class, e.g.
Constructor Details
#initialize(name, app = Application.app_for(Dir.pwd)) ⇒ Generator
Input the generator’s name and the Application to generate for
21 22 23 24 25 |
# File 'lib/ptero/generator.rb', line 21 def initialize(name,app=Application.app_for(Dir.pwd)) @name = name @dir = Pathname.new(app.dir) @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
27 28 29 |
# File 'lib/ptero/generator.rb', line 27 def app @app end |
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
27 28 29 |
# File 'lib/ptero/generator.rb', line 27 def dir @dir end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/ptero/generator.rb', line 27 def name @name end |
Class Method Details
.const_missing(const_name) ⇒ Object
Autoload Generators
138 139 140 141 142 143 144 145 146 |
# File 'lib/ptero/generator.rb', line 138 def const_missing(const_name) # Require the generator require "#{__dir__}/generators/#{const_name.downcase}.rb" return const_get const_name if const_defined? const_name super # If we couldn't load the file, throw an error rescue LoadError super end |
Instance Method Details
#content ⇒ String
Return the content of the file to be generated by inputting content_params into an erubis template
118 119 120 121 122 123 |
# File 'lib/ptero/generator.rb', line 118 def content File.open(template_path, 'r') do |file| eruby = Erubis::Eruby.new(file.read) eruby.evaluate(content_params) end end |
#content_params ⇒ Generator
The context for generating a template, default to self
126 127 128 |
# File 'lib/ptero/generator.rb', line 126 def content_params self end |
#extension ⇒ String
The extension of the file to be generated
37 38 39 |
# File 'lib/ptero/generator.rb', line 37 def extension 'txt' end |
#filename ⇒ String
The filename of the generated file
31 32 33 |
# File 'lib/ptero/generator.rb', line 31 def filename "#{@name}.#{extension}" end |
#generate ⇒ Generator
Generate a file and print the location of the generated file
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ptero/generator.rb', line 73 def generate loc = location raise Ptero::Exception::GeneratorException, "Generator is already generated: #{self}" if generated? unless loc.dirname.exist? loc.dirname.descend do |dir| Dir.mkdir dir unless dir.exist? end end File.open(loc,'w') do |file| file.puts content end puts "GENERATE - #{self}".green self end |
#generated? ⇒ Boolean
Find out if this Generator’s file is generated
101 102 103 |
# File 'lib/ptero/generator.rb', line 101 def generated? File.exist? location end |
#location ⇒ String
The fully-qualified filename of the file to be generated by this object.
61 62 63 |
# File 'lib/ptero/generator.rb', line 61 def location dir.join(path).join(filename) end |
#path ⇒ String
Default path to write to, used along with filename to determine the destination of the generated file\
55 56 57 |
# File 'lib/ptero/generator.rb', line 55 def path '' end |
#reload ⇒ Generator
Remove and regenerate and print the regenerated file
107 108 109 110 111 112 113 114 |
# File 'lib/ptero/generator.rb', line 107 def reload Mute::IO.capture_stdout do remove if generated? generate end puts "RELOAD - #{self}".blue self end |
#remove ⇒ Generator
Remove the file corresponding to self and print its location
91 92 93 94 95 96 97 |
# File 'lib/ptero/generator.rb', line 91 def remove loc = location raise Ptero::Exception::GeneratorException, "Cannot remove because generator is already generated: #{self}" unless generated? File.unlink(loc); puts "REMOVE - #{self}".red self end |
#template_path ⇒ String
The path to the directory where Ptero templates are stored
67 68 69 |
# File 'lib/ptero/generator.rb', line 67 def template_path Pathname.new("#{TEMPLATE_PATH}/#{self.class.name.split('::').last.downcase}.#{extension}.erb") end |
#to_s ⇒ String
Simple string representation of this object, represented by the unqualified class name and filename of the current object
49 50 51 |
# File 'lib/ptero/generator.rb', line 49 def to_s "[#{type} - #{filename}]" end |
#type ⇒ String
The unqualified name of the class, e.g. ‘Controller’ for an object of class Ptero::Generator::ControllerGenerator
43 44 45 |
# File 'lib/ptero/generator.rb', line 43 def type self.class.name.split('::').last end |