Class: Retrospec::Puppet::Generators::FactGenerator

Inherits:
Retrospec::Plugins::V1::Plugin
  • Object
show all
Defined in:
lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_path, spec_object = {}) ⇒ FactGenerator

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



11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#config_dataObject (readonly)

Returns the value of attribute config_data.



7
8
9
# File 'lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb', line 7

def config_data
  @config_data
end

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb', line 7

def context
  @context
end

#fact_nameObject (readonly)

Returns the value of attribute fact_name.



7
8
9
# File 'lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb', line 7

def fact_name
  @fact_name
end

#module_pathObject (readonly)

Returns the value of attribute module_path.



7
8
9
# File 'lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb', line 7

def module_path
  @module_path
end

#template_dirObject (readonly)

the template directory located inside the your retrospec plugin gem you should not have to modify this unless you move the templates directory for now we are going to choose the correct template directory that contains the templates



87
88
89
# File 'lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb', line 87

def template_dir
  @template_dir
end

Class Method Details

.run_cli(global_opts, args = ARGV) ⇒ Object

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb', line 24

def self.run_cli(global_opts, args=ARGV)
  sub_command_opts = Trollop.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]
    Trollop.educate
    exit 1
  end
  plugin_data = global_opts.merge(sub_command_opts)
  Retrospec::Puppet::Generators::FactGenerator.new(plugin_data[:module_path], plugin_data)
end

Instance Method Details

#fact_filesObject

returns an array of fact files found in the facter directory



63
64
65
# 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_pathObject



52
53
54
# 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_dirObject



44
45
46
# 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_dirObject



48
49
50
# 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_fileObject

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



57
58
59
60
# 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_filesObject

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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