Class: NginxStage::Generator

Inherits:
Object
  • Object
show all
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.

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • opts (Hash) (defaults to: {})

    various options for controlling the behavior of the generator



78
79
80
81
82
83
84
85
86
87
# File 'lib/nginx_stage/generator.rb', line 78

def initialize(opts = {})
  self.class.options.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

._optionsHash

Returns a hash of options with callbacks that return a hash of their attributes

Returns:

  • (Hash)

    a hash of options with the corresponding callback



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

def self._options
  @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

Parameters:

  • name (Symbol)

    unique key defining callback method

Yields:

  • The body of the generator’s callback



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

Parameters:

  • name (Symbol)

    unique key defining option

Yields:

  • The body of the option’s callback which should return a hash



39
40
41
42
# File 'lib/nginx_stage/generator.rb', line 39

def self.add_option(name, &block)
  attr_reader name
  self._options[name] = block
end

.desc(desc = nil) ⇒ String

Returns the description of generator

Returns:

  • (String)

    description of generator



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

def self.desc(desc = nil)
  @desc ||= desc
end

Returns the footer description of generator

Returns:

  • (String)

    footer description of generator



73
74
75
# File 'lib/nginx_stage/generator.rb', line 73

def self.footer(footer = nil)
  @footer ||= footer
end

.hooksHash

Returns a hash of callback methods in the order they will be invoked

Returns:

  • (Hash)

    the callback methods



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

def self.hooks
  @hooks ||= from_superclass(:hooks, {})
end

.optionsHash

Returns a hash of options that point to a hash of their attributes

Returns:

  • (Hash)

    a hash of options with the corresponding hash of attributes



61
62
63
# File 'lib/nginx_stage/generator.rb', line 61

def self.options
  Hash[self._options.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

Parameters:

  • name (Symbol)

    unique key defining callback method



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

Parameters:

  • name (Symbol)

    unique key defining option



47
48
49
50
# File 'lib/nginx_stage/generator.rb', line 47

def self.rem_option(name)
  undef name
  self._options.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

Parameters:

  • destination (String)

    the relative path to the destination file

  • data (String) (defaults to: "")

    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

Parameters:

  • destination (String)

    the directory path

  • mode (Integer) (defaults to: 0755)

    the mode to set the directory as



119
120
121
# File 'lib/nginx_stage/generator.rb', line 119

def empty_directory(destination, mode: 0755)
  FileUtils.mkdir_p destination, mode: mode
end

#invokevoid

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

Parameters:

  • source (String)

    the relative path to the source file

  • destination (String)

    the relative path to the destination file



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