Class: Retrospec::Puppet::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/retrospec/plugins/v1/plugin/generators/parsers/type.rb

Class Method Summary collapse

Class Method Details

.load_type(type_file, provider_file = nil) ⇒ Object

loads the type_file and provider_file if given determines if type or provider is being used and evals the code if the provider_file is not loadable we cannot build a full context for the template to be rendered with so we use a default context instead



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/retrospec/plugins/v1/plugin/generators/parsers/type.rb', line 19

def self.load_type(type_file, provider_file = nil)
  if provider_file
    begin
      file = provider_file
      @model = OpenStruct.new(:name => File.basename(file, '.rb'), :file => file,
                              :class_methods => [], :instance_methods => [],
                              :properties => [], :parameters => [])
      require type_file
      require provider_file
      t = eval(File.read(file))
      @model.parameters = t.resource_type.parameters
      @model.properties = t.resource_type.validproperties
      @model.name = t.name
      @model.class_methods = t.methods(false)
      @model.instance_methods = t.instance_methods(false)
    rescue LoadError => e
      puts "#{e.message}, generating empty file".fatal
    rescue NameError => e
      puts "#{e.message}, is this valid provider code?".fatal
    rescue NoMethodError => e
      puts "#{e.message}, is this valid provider code?".fatal
    rescue ::Puppet::Context::UndefinedBindingError => e
      puts "There was an issue with loading the provider code for #{file}, skipping".fatal
    end
    @model
  else
    begin
      require type_file
      file = type_file
      @model = OpenStruct.new(:name => File.basename(file, '.rb'), :file => file,
                              :properties => [], :instance_methods => [],
                              :parameters => [], :methods_defined => [])
      t = eval(File.read(file))
      @model.name = t.name
      @model.parameters = t.parameters
      @model.properties = t.properties.collect(&:name)
      @model.instance_methods = t.instance_methods(false)
      @model
    rescue LoadError => e
      puts "#{e.message}, generating empty file".fatal
    rescue NameError => e
      puts "#{e.message}, is this valid type code?".fatal
    rescue NoMethodError => e
      puts "#{e.message}, is this valid type code?".fatal
    end
  end
  @model
end

.newtype(name, _options = {}, &_block) ⇒ Object

I don’t know of a better way to get the name of the type



73
74
75
# File 'lib/retrospec/plugins/v1/plugin/generators/parsers/type.rb', line 73

def self.newtype(name, _options = {}, &_block)
  ::Puppet::Type.type(name)
end

.type(name, _options = {}, &_block) ⇒ Object



68
69
70
# File 'lib/retrospec/plugins/v1/plugin/generators/parsers/type.rb', line 68

def self.type(name, _options = {}, &_block)
  ::Puppet::Type.type(name)
end