Class: NginxStage::Generator
- Inherits:
-
Object
- Object
- NginxStage::Generator
- Extended by:
- GeneratorHelpers
- Defined in:
- lib/nginx_stage/generator.rb
Overview
Base class for objects that add new sub-commands to NginxStage. Generator is basically a class with helper methods and the ability to invoke all callback methods in a sequence.
Direct Known Subclasses
AppCleanGenerator, AppConfigGenerator, AppListGenerator, AppResetGenerator, NginxCleanGenerator, NginxListGenerator, NginxProcessGenerator, NginxShowGenerator, PunConfigGenerator
Class Method Summary collapse
-
._options ⇒ Hash
Returns a hash of options with callbacks that return a hash of their attributes.
-
.add_hook(name) { ... } ⇒ void
Adds a new hook method that is invoked in the order it is defined.
-
.add_option(name) { ... } ⇒ void
Adds a new option expected from CLI and treats it as an attribute.
-
.desc(desc = nil) ⇒ String
Returns the description of generator.
-
.footer(footer = nil) ⇒ String
Returns the footer description of generator.
-
.hooks ⇒ Hash
Returns a hash of callback methods in the order they will be invoked.
-
.options ⇒ Hash
Returns a hash of options that point to a hash of their attributes.
-
.rem_hook(name) ⇒ void
Removes a hook method from the callback chain.
-
.rem_option(name) ⇒ void
Removes an option expected from the CLI and removes attribute method.
Instance Method Summary collapse
-
#create_file(destination, data = "") ⇒ void
Create a new file at the destination path with the given data.
-
#empty_directory(destination, mode: 0755) ⇒ void
Create an empty directory if it doesn’t already exist.
-
#initialize(opts = {}) ⇒ Generator
constructor
A new instance of Generator.
-
#invoke ⇒ void
Invokes all the callbacks in the order they are defined in the Generator.hooks hash.
-
#template(source, destination) ⇒ void
Gets an ERB template at the relative source, executes it and makes a copy at the relative destination.
Methods included from GeneratorHelpers
add_skip_nginx_support, add_sub_uri_support, add_user_support
Constructor Details
#initialize(opts = {}) ⇒ Generator
Returns a new instance of Generator.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/nginx_stage/generator.rb', line 78 def initialize(opts = {}) self.class..each do |k,v| value = opts.fetch(k) do raise MissingOption, "missing option: #{k}" if v[:required] v[:default] end value = v[:before_init].call(value) if v[:before_init] instance_variable_set("@#{k}", value) end end |
Class Method Details
._options ⇒ Hash
Returns a hash of options with callbacks that return a hash of their attributes
55 56 57 |
# File 'lib/nginx_stage/generator.rb', line 55 def self. @options ||= from_superclass(:_options, {}) end |
.add_hook(name) { ... } ⇒ void
This method returns an undefined value.
Adds a new hook method that is invoked in the order it is defined
18 19 20 |
# File 'lib/nginx_stage/generator.rb', line 18 def self.add_hook(name, &block) self.hooks[name] = block end |
.add_option(name) { ... } ⇒ void
This method returns an undefined value.
Adds a new option expected from CLI and treats it as an attribute
39 40 41 42 |
# File 'lib/nginx_stage/generator.rb', line 39 def self.add_option(name, &block) attr_reader name self.[name] = block end |
.desc(desc = nil) ⇒ String
Returns the description of generator
67 68 69 |
# File 'lib/nginx_stage/generator.rb', line 67 def self.desc(desc = nil) @desc ||= desc end |
.footer(footer = nil) ⇒ String
Returns the footer description of generator
73 74 75 |
# File 'lib/nginx_stage/generator.rb', line 73 def self.( = nil) @footer ||= end |
.hooks ⇒ Hash
Returns a hash of callback methods in the order they will be invoked
31 32 33 |
# File 'lib/nginx_stage/generator.rb', line 31 def self.hooks @hooks ||= from_superclass(:hooks, {}) end |
.options ⇒ Hash
Returns a hash of options that point to a hash of their attributes
61 62 63 |
# File 'lib/nginx_stage/generator.rb', line 61 def self. Hash[self..map { |k,v| [k, v.call] }] end |
.rem_hook(name) ⇒ void
This method returns an undefined value.
Removes a hook method from the callback chain
25 26 27 |
# File 'lib/nginx_stage/generator.rb', line 25 def self.rem_hook(name) self.hooks.delete(name) end |
.rem_option(name) ⇒ void
This method returns an undefined value.
Removes an option expected from the CLI and removes attribute method
47 48 49 50 |
# File 'lib/nginx_stage/generator.rb', line 47 def self.rem_option(name) undef name self..delete(name) end |
Instance Method Details
#create_file(destination, data = "") ⇒ void
This method returns an undefined value.
Create a new file at the destination path with the given data
110 111 112 113 |
# File 'lib/nginx_stage/generator.rb', line 110 def create_file(destination, data = "") empty_directory File.dirname(destination) File.open(destination, "wb", 0644) { |f| f.write data } end |
#empty_directory(destination, mode: 0755) ⇒ void
This method returns an undefined value.
Create an empty directory if it doesn’t already exist
119 120 121 |
# File 'lib/nginx_stage/generator.rb', line 119 def empty_directory(destination, mode: 0755) FileUtils.mkdir_p destination, mode: mode end |
#invoke ⇒ void
This method returns an undefined value.
Invokes all the callbacks in the order they are defined in the hooks hash
92 93 94 |
# File 'lib/nginx_stage/generator.rb', line 92 def invoke self.class.hooks.each {|k,v| self.instance_eval(&v)} end |
#template(source, destination) ⇒ void
This method returns an undefined value.
Gets an ERB template at the relative source, executes it and makes a copy at the relative destination
101 102 103 104 |
# File 'lib/nginx_stage/generator.rb', line 101 def template(source, destination) data = File.read File.join(NginxStage.template_root, source) create_file destination, render(data) end |