Class: PuppetResource

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-lint/plugins/check_wmf_styleguide.rb

Overview

Class to manage puppet resources. See how we extend PuppetLint::Lexer::Token below to understand how we filter tokens within a parsed resource.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_hash) ⇒ PuppetResource

Returns a new instance of PuppetResource.



9
10
11
12
13
14
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 9

def initialize(resource_hash)
  # Input should be a resource coming from
  # the resource index
  @resource = resource_hash
  @params = parse_params
end

Instance Attribute Details

#profile_moduleObject

Returns the value of attribute profile_module.



7
8
9
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 7

def profile_module
  @profile_module
end

#role_moduleObject

Returns the value of attribute role_module.



7
8
9
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 7

def role_module
  @role_module
end

Instance Method Details

#class?Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 70

def class?
  # True if this is a class,
  @resource[:type] == :CLASS
end

#declared_classesObject



125
126
127
128
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 125

def declared_classes
  # Returns an array of all the declared classes
  @resource[:tokens].map(&:declared_class).compact
end

#declared_resourcesObject



130
131
132
133
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 130

def declared_resources
  # Returns an array of all the declared classes
  @resource[:tokens].select(&:declared_type?)
end

#filenameObject



85
86
87
88
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 85

def filename
  # File name of the resource
  @resource[:filename]
end

#included_classesObject



120
121
122
123
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 120

def included_classes
  # Returns an array of all the classes included (with require/include)
  @resource[:tokens].map(&:included_class).compact
end

#legacy_hiera_callsObject



110
111
112
113
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 110

def legacy_hiera_calls
  # Returns an array of all the tokens referencing calls to hiera
  @resource[:tokens].select(&:legacy_hiera?)
end

#legacy_validate_callsObject



115
116
117
118
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 115

def legacy_validate_calls
  # Returns an array of all the tokens referencing calls to a stdlib legacy validate function
  @resource[:tokens].select(&:legacy_validate?)
end

#lookup_callsObject



105
106
107
108
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 105

def lookup_calls
  # Returns an array of all the tokens referencing calls to lookup
  @resource[:tokens].select(&:lookup?)
end

#module_nameObject



90
91
92
93
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 90

def module_name
  # Module containing this resource
  name.split('::')[0]
end

#nameObject



75
76
77
78
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 75

def name
  # Extract a normalized resource name (without the :: prefix if present)
  @resource[:name_token].value.gsub(/^::/, '')
end

#paramsObject

rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength



55
56
57
58
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 55

def params
  # Lazy-load and return all the parameters of the resource
  @params || parse_params
end

#parse_paramsObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength



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
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 25

def parse_params
  # Parse parameters and return a hash containing
  # the parameter name as a key and a hash of tokens as value:
  #  :param => the parameter token
  #  :value => All the code tokens that represent the value of the parameter
  res = {}
  current_param = nil
  in_value = false
  return res unless @resource[:param_tokens]
  @resource[:param_tokens].each do |token|
    case token.type
    when :VARIABLE
      current_param = token.value
      res[current_param] ||= { param: token, value: [] }
    when :COMMA
      current_param = nil
      in_value = false
    when :EQUALS
      in_value = true
    when *PuppetLint::Lexer::FORMATTING_TOKENS
      # Skip non-code tokens
      next
    else
      res[current_param][:value] << token if in_value && token
    end
  end
  res
end

#pathObject



80
81
82
83
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 80

def path
  # Path of the resource
  @resource[:path]
end

#profile?Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 95

def profile?
  # True if the resource is in the profile module
  class? && (module_name == profile_module)
end

#resource?(name) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 135

def resource?(name)
  # Arguments:
  #   name (string) Name of the resource we want to search
  # Returns an array of all the defines of the specified resource
  @resource[:tokens].select { |t| t.declared_type? && t.value.gsub(/^::/, '') == name }
end

#role?Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 100

def role?
  # True if the resource is in the role module
  class? && (module_name == role_module)
end

#typeObject



16
17
18
19
20
21
22
# File 'lib/puppet-lint/plugins/check_wmf_styleguide.rb', line 16

def type
  if class?
    'class'
  else
    'defined type'
  end
end