Class: Retrospec::Puppet::Generators::BaseGenerator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



9
10
11
12
13
14
15
# 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

Instance Attribute Details

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#generator_template_dir_nameObject (readonly)

Returns the value of attribute generator_template_dir_name.



5
6
7
# File 'lib/retrospec/plugins/v1/plugin/generators/base_generator.rb', line 5

def generator_template_dir_name
  @generator_template_dir_name
end

#plural_nameObject (readonly)

Returns the value of attribute plural_name.



5
6
7
# File 'lib/retrospec/plugins/v1/plugin/generators/base_generator.rb', line 5

def plural_name
  @plural_name
end

#singular_nameObject (readonly)

Returns the value of attribute singular_name.



5
6
7
# File 'lib/retrospec/plugins/v1/plugin/generators/base_generator.rb', line 5

def singular_name
  @singular_name
end

#template_dirObject (readonly)

returns the path to the templates first looks inside the external templates directory for specific file then looks inside the gem path templates directory, which is really only useful when developing new templates.



110
111
112
# File 'lib/retrospec/plugins/v1/plugin/generators/base_generator.rb', line 110

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 optimist.rubyforge.org all options here are available in the config passed into config object returns the parameters



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Method Details

#generate_file_name(iname) ⇒ Object

returns the base filename of the type



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

Raises:

  • (NotImplementedError)


41
42
43
# 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) ⇒ String

Returns - a generated path.

Parameters:

  • file_name
    • the base name of the file to create

  • iname (defaults to: item_name)
    • the type name or name of item

Returns:

  • (String)
    • a generated path



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

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/retrospec/plugins/v1/plugin/generators/base_generator.rb', line 45

def generate_spec_files
  raise NotImplementedError
end

#get_bindingObject



121
122
123
# File 'lib/retrospec/plugins/v1/plugin/generators/base_generator.rb', line 121

def get_binding
  binding
end

#item_nameObject



90
91
92
# File 'lib/retrospec/plugins/v1/plugin/generators/base_generator.rb', line 90

def item_name
  context.name
end

#item_pathObject



94
95
96
# 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_pathObject

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



51
52
53
54
55
# 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_pathObject



102
103
104
# File 'lib/retrospec/plugins/v1/plugin/generators/base_generator.rb', line 102

def lib_path
  File.join(module_path, 'lib', 'puppet', plural_name)
end

#loggerObject



17
18
19
# File 'lib/retrospec/plugins/v1/plugin/generators/base_generator.rb', line 17

def logger
  Retrospec::Plugins::V1::Puppet.logger
end

#runObject

run is the main method that gets called automatically



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

def run
  generate_lib_files
end

#spec_pathObject



98
99
100
# 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