class Retrospec::Puppet::Generators::BaseGenerator

Attributes

context[RW]
generator_template_dir_name[R]
plural_name[R]
singular_name[R]
template_dir[R]

Public Class Methods

new(module_path, spec_object = {}) click to toggle source

retrospec will initilalize this class so its up to you to set any additional variables you need to get the job done.

Calls superclass method
# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 9
def initialize(module_path, spec_object = {})
  super
  # below is the Spec Object which serves as a context for template rendering
  # you will need to initialize this object, so the erb templates can get the binding
  # the SpecObject can be customized to your liking as its different for every plugin gem.
  @context = OpenStruct.new(spec_object)
end
run_cli(global_opts, args=ARGV) click to toggle source

used to display subcommand options to the cli the global options are passed in for your usage optimist.rubyforge.org all options here are available in the config passed into config object returns the parameters

# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 26
        def self.run_cli(global_opts, args=ARGV)
          sub_command_opts = Optimist.options(args) do
            banner <<-EOS
            ""
            EOS
            opt :name, "The name of the item you wish to create", :type => :string, :required => true, :short => '-n'
          end
          unless sub_command_opts[:name]
            Optimist.educate
            exit 1
          end
          plugin_data = global_opts.merge(sub_command_opts)
          plugin_data
        end

Public Instance Methods

generate_file_name(iname) click to toggle source

returns the base filename of the type

# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 58
def generate_file_name(iname)
  tokens = iname.split('::')
  file_name = tokens.pop
end
generate_lib_files() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 41
def generate_lib_files
  raise NotImplementedError
end
generate_path(file_name, iname = item_name) click to toggle source

@return [String] - a generated path @param file_name - the base name of the file to create @param iname - the type name or name of item

# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 66
def generate_path(file_name, iname = item_name)
  tokens = iname.split('::')
  # if there are only two tokens ie. tomcat::params we dont need to create a subdirectory
  if tokens.count > 2
    # this is a deep level resource ie. tomcat::config::server::connector
    # however we don't need the tomcat directory so we can just remove it
    # this should leave us with config/server/connector_spec.rb
    tokens.delete_at(0)
    # remove the last token since its the class name
    tokens.pop
    # so lets make a directory structure out of it
    dir_name = File.join(tokens) # config/server
    dir_name = File.join(dir_name, file_name) # spec/classes/tomcat/config/server
  else
    dir_name = File.join(file_name)
  end
  dir_name.downcase
end
generate_spec_files() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 45
def generate_spec_files
  raise NotImplementedError
end
get_binding() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 121
def get_binding
  binding
end
item_name() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 90
def item_name
  context.name
end
item_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 94
def item_path
  File.join(lib_path, "#{item_name}.rb")
end
item_spec_path() click to toggle source

generates a file path for spec tests based on the resource name. An added option is to generate directory names for each parent resource as a default option

# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 51
def item_spec_path
  file_name = generate_file_name(item_name.downcase)
  path = generate_path("#{file_name}_spec.rb", item_name)
  File.join(spec_path, path )
end
lib_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 102
def lib_path
  File.join(module_path, 'lib', 'puppet', plural_name)
end
logger() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 17
def logger
  Retrospec::Plugins::V1::Puppet.logger
end
run() click to toggle source

run is the main method that gets called automatically

# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 86
def run
  generate_lib_files
end
spec_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/base_generator.rb, line 98
def spec_path
  File.join(module_path, 'spec', 'unit', 'puppet', plural_name)
end