Class: Ptero::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/ptero/generator.rb

Overview

An object that generates files for an application

Defined Under Namespace

Classes: ApplicationJavascriptGenerator, ApplicationStylesheetGenerator, ConfigGenerator, ControllerGenerator, HTAccessGenerator, JavascriptGenerator, LandingGenerator, LayoutGenerator, LoadAllGenerator, ModelGenerator, PHPClassGenerator, PHPGenerator, PHPInfoGenerator, PageGenerator, PageNotFoundGenerator, RoutesGenerator, SetupGenerator, StylesheetGenerator, TwigGenerator, ViewGenerator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, app = Application.app_for(Dir.pwd)) ⇒ Generator

Input the generator’s name and the Application to generate for

Parameters:

  • name (String)

    the generator’s name

  • app (Application) (defaults to: Application.app_for(Dir.pwd))

    the application in which to generate files



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

#appObject (readonly)

Returns the value of attribute app.



27
28
29
# File 'lib/ptero/generator.rb', line 27

def app
  @app
end

#dirObject (readonly)

Returns the value of attribute dir.



27
28
29
# File 'lib/ptero/generator.rb', line 27

def dir
  @dir
end

#nameObject (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



134
135
136
137
138
139
140
141
# File 'lib/ptero/generator.rb', line 134

def const_missing(const_name)
  # Require the generator
  require "#{__dir__}/generators/#{const_name.downcase}.rb"
  return const_get const_name
# If we couldn't load the file, throw an error
rescue LoadError
  super
end

Instance Method Details

#contentString

Return the content of the file to be generated by inputting content_params into an erubis template

Returns:

  • (String)

    the content of the file to be generated



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_paramsGenerator

The context for generating a template, default to self

Returns:



126
127
128
# File 'lib/ptero/generator.rb', line 126

def content_params
  self
end

#extensionString

The extension of the file to be generated

Returns:

  • (String)

    “txt”



37
38
39
# File 'lib/ptero/generator.rb', line 37

def extension
  'txt'
end

#filenameString

The filename of the generated file

Returns:

  • (String)

    an unqualifed filename, name and extension



31
32
33
# File 'lib/ptero/generator.rb', line 31

def filename
  "#{@name}.#{extension}"
end

#generateGenerator

Generate a file and print the location of the generated file

Returns:

Raises:



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

Returns:

  • (Boolean)

    Does the file exist?



101
102
103
# File 'lib/ptero/generator.rb', line 101

def generated?
  File.exist? location
end

#locationString

The fully-qualified filename of the file to be generated by this object.

Returns:

  • (String)

    a fully-qualified pathname to 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

#pathString

Default path to write to, used along with filename to determine the destination of the generated file\

Returns:

  • (String)

    the empty string



55
56
57
# File 'lib/ptero/generator.rb', line 55

def path
  ''
end

#reloadGenerator

Remove and regenerate and print the regenerated file

Returns:



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

#removeGenerator

Remove the file corresponding to self and print its location

Returns:

Raises:



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_pathString

The path to the directory where Ptero templates are stored

Returns:

  • (String)

    the aforementioned path



67
68
69
# File 'lib/ptero/generator.rb', line 67

def template_path
  Pathname.new("#{Ptero::TEMPLATE_PATH}/#{self.class.name.split('::').last.downcase}.#{extension}.erb")
end

#to_sString

Simple string representation of this object, represented by the unqualified class name and filename of the current object

Returns:

  • (String)

    a string representation of this object, of type “[type - filename]”



49
50
51
# File 'lib/ptero/generator.rb', line 49

def to_s
  "[#{type} - #{filename}]"
end

#typeString

The unqualified name of the class, e.g. ‘Controller’ for an object of class Ptero::Generator::ControllerGenerator

Returns:

  • (String)

    the unqualified name of the class



43
44
45
# File 'lib/ptero/generator.rb', line 43

def type
  self.class.name.split('::').last
end