Skip to content

Commit d75a134

Browse files
committed
(maint) clean up unneeded define_method calls
These calls add to the complexity of the code without adding any value. Since the introduction of the `type_definition` there are very few reasons for methods to remain defined as a block. This commit changes all of them and a few remaining carte blanche references to `definition`.
1 parent e90f638 commit d75a134

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

lib/puppet/resource_api.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def type_definition
9191
apply_to_device
9292
end
9393

94-
define_method(:initialize) do |attributes|
94+
def initialize(attributes)
9595
# $stderr.puts "A: #{attributes.inspect}"
9696
if attributes.is_a? Puppet::Resource
9797
@title = attributes.title
@@ -119,7 +119,7 @@ def type_definition
119119
# the `Puppet::Resource::Ral.find` method, when `instances` does not return a match, uses a Hash with a `:name` key to create
120120
# an "absent" resource. This is often hit by `puppet resource`. This needs to work, even if the namevar is not called `name`.
121121
# This bit here relies on the default `title_patterns` (see below) to match the title back to the first (and often only) namevar
122-
if definition[:attributes][:name].nil? && attributes[:title].nil?
122+
if type_definition.attributes[:name].nil? && attributes[:title].nil?
123123
attributes[:title] = attributes.delete(:name)
124124
if attributes[:title].nil? && !type_definition.namevars.empty?
125125
attributes[:title] = @title
@@ -137,7 +137,7 @@ def to_resource
137137
to_resource_shim(super)
138138
end
139139

140-
define_method(:to_resource_shim) do |resource|
140+
def to_resource_shim(resource)
141141
resource_hash = Hash[resource.keys.map { |k| [k, resource[k]] }]
142142
resource_hash[:title] = resource.title
143143
ResourceShim.new(resource_hash, type_definition.name, type_definition.namevars, type_definition.attributes, catalog)
@@ -244,7 +244,7 @@ def to_resource
244244
end
245245
end
246246

247-
define_singleton_method(:instances) do
247+
def self.instances
248248
# puts 'instances'
249249
# force autoloading of the provider
250250
provider(type_definition.name)
@@ -268,7 +268,7 @@ def to_resource
268268
end
269269
end
270270

271-
define_method(:refresh_current_state) do
271+
def refresh_current_state
272272
@rsapi_current_state = if type_definition.feature?('simple_get_filter')
273273
my_provider.get(context, [title]).find { |h| namevar_match?(h) }
274274
else
@@ -290,7 +290,7 @@ def cache_current_state(resource_hash)
290290
strict_check(@rsapi_current_state) if type_definition.feature?('canonicalize')
291291
end
292292

293-
define_method(:retrieve) do
293+
def retrieve
294294
refresh_current_state unless @rsapi_current_state
295295

296296
Puppet.debug("Current State: #{@rsapi_current_state.inspect}")
@@ -304,13 +304,13 @@ def cache_current_state(resource_hash)
304304
result
305305
end
306306

307-
define_method(:namevar_match?) do |item|
307+
def namevar_match?(item)
308308
context.type.namevars.all? do |namevar|
309309
item[namevar] == @parameters[namevar].value if @parameters[namevar].respond_to? :value
310310
end
311311
end
312312

313-
define_method(:flush) do
313+
def flush
314314
raise_missing_attrs
315315

316316
# puts 'flush'
@@ -328,7 +328,7 @@ def cache_current_state(resource_hash)
328328
# enforce init_only attributes
329329
if Puppet.settings[:strict] != :off && @rsapi_current_state && (@rsapi_current_state[:ensure] == 'present' && target_state[:ensure] == 'present')
330330
target_state.each do |name, value|
331-
next unless definition[:attributes][name][:behaviour] == :init_only && value != @rsapi_current_state[name]
331+
next unless type_definition.attributes[name][:behaviour] == :init_only && value != @rsapi_current_state[name]
332332
message = "Attempting to change `#{name}` init_only attribute value from `#{@rsapi_current_state[name]}` to `#{value}`"
333333
case Puppet.settings[:strict]
334334
when :warning
@@ -350,17 +350,17 @@ def cache_current_state(resource_hash)
350350
@rsapi_current_state = target_state
351351
end
352352

353-
define_method(:raise_missing_attrs) do
353+
def raise_missing_attrs
354354
error_msg = "The following mandatory attributes were not provided:\n * " + @missing_attrs.join(", \n * ")
355355
raise Puppet::ResourceError, error_msg if @missing_attrs.any? && (value(:ensure) != :absent && !value(:ensure).nil?)
356356
end
357357

358-
define_method(:raise_missing_params) do
358+
def raise_missing_params
359359
error_msg = "The following mandatory parameters were not provided:\n * " + @missing_params.join(", \n * ")
360360
raise Puppet::ResourceError, error_msg
361361
end
362362

363-
define_method(:strict_check) do |current_state|
363+
def strict_check(current_state)
364364
return if Puppet.settings[:strict] == :off
365365

366366
# if strict checking is on we must notify if the values are changed by canonicalize
@@ -374,7 +374,7 @@ def cache_current_state(resource_hash)
374374
#:nocov:
375375
# codecov fails to register this multiline as covered, even though simplecov does.
376376
message = <<MESSAGE.strip
377-
#{definition[:name]}[#{@title}]#get has not provided canonicalized values.
377+
#{type_definition.name}[#{@title}]#get has not provided canonicalized values.
378378
Returned values: #{current_state.inspect}
379379
Canonicalized values: #{state_clone.inspect}
380380
MESSAGE
@@ -387,7 +387,7 @@ def cache_current_state(resource_hash)
387387
raise Puppet::DevError, message
388388
end
389389

390-
return nil
390+
nil
391391
end
392392

393393
define_singleton_method(:context) do
@@ -398,9 +398,9 @@ def context
398398
self.class.context
399399
end
400400

401-
define_singleton_method(:title_patterns) do
402-
@title_patterns ||= if definition.key? :title_patterns
403-
parse_title_patterns(definition[:title_patterns])
401+
def self.title_patterns
402+
@title_patterns ||= if type_definition.definition.key? :title_patterns
403+
parse_title_patterns(type_definition.definition[:title_patterns])
404404
else
405405
[[%r{(.*)}m, [[type_definition.namevars.first]]]]
406406
end

0 commit comments

Comments
 (0)