class Retrospec::Puppet::Generators::FactGenerator

Attributes

config_data[R]
context[R]
fact_name[R]
module_path[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.

# File lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb, line 11
def initialize(module_path, spec_object = {})
  # 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.
  @module_path = module_path
  @config_data = spec_object
  @context = OpenStruct.new(:fact_name => spec_object[:name])
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

# File lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb, line 24
        def self.run_cli(global_opts, args=ARGV)
          sub_command_opts = Optimist.options(args) do
            banner <<-EOS
Generates a new fact with the given name

            EOS
            opt :name, 'The name of the fact you wish to create', :type => :string, :require => :true, :short => '-n'
          end
          unless sub_command_opts[:name]
            Optimist.educate
            exit 1
          end
          plugin_data = global_opts.merge(sub_command_opts)
          Retrospec::Puppet::Generators::FactGenerator.new(plugin_data[:module_path], plugin_data)
        end

Public Instance Methods

fact_files() click to toggle source

returns an array of fact files found in the facter directory

# File lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb, line 63
def fact_files
  @fact_files ||= Dir.glob(File.join(facter_dir, '*.rb')).sort
end
fact_name_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb, line 52
def fact_name_path
  File.join(facter_dir, "#{fact_name}.rb")
end
facter_dir() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb, line 44
def facter_dir
  @facter_dir ||= File.join(module_path, 'lib', 'facter')
end
facter_spec_dir() click to toggle source
# File lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb, line 48
def facter_spec_dir
  @facter_spec_dir ||= File.join(module_path, 'spec', 'unit', 'facter')
end
generate_fact_file() click to toggle source

generates a fact file with the given name based on the template in the templates directory

# File lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb, line 57
def generate_fact_file
  safe_create_template_file(fact_name_path, File.join(template_dir, 'fact.rb.retrospec.erb'), context)
  generate_fact_spec_files
end
generate_fact_spec_files() click to toggle source

generates spec files for each fact defined in the fact file returns a array of generated spec files

# File lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb, line 69
def generate_fact_spec_files
  spec_files = []
  template_file = File.join(template_dir, 'fact_spec.rb.retrospec.erb')
  fact_files.each do | fact_file|
    fact_file_data = Retrospec::Puppet::Generators::Facter.load_fact(fact_file)
    fact_file_data.facts.each do |name, fact_data|
      # because many facts can be in a single file we want to create a unique file for each fact
      fact_spec_path = File.join(facter_spec_dir, "#{name}_spec.rb")
      safe_create_template_file(fact_spec_path,template_file , fact_data)
      spec_files << fact_spec_path
    end
  end
  spec_files
end