Class: Puppet::Provider::AixObject::MappedObject

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/provider/aix_object.rb

Overview

Class representing a MappedObject, which can either be an AIX attribute or a Puppet property. This class lets us write something like:

attribute = mappings[:aix_attribute][:uid]
attribute.name
attribute.convert_property_value(uid)

property = mappings[:puppet_property][:id]
property.name
property.convert_attribute_value(id)

NOTE: This is an internal class specific to AixObject. It is not meant to be used anywhere else. That's why we do not have any validation code in here.

NOTE: See the comments in the class-level mappings method to understand what we mean by pure and impure conversion functions.

NOTE: The 'mapping' code, including this class, could possibly be moved to a separate module so that it can be re-used in some of our other providers. See PUP-9082.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, conversion_fn, conversion_fn_code) ⇒ MappedObject

Returns a new instance of MappedObject


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet/provider/aix_object.rb', line 30

def initialize(name, conversion_fn, conversion_fn_code)
  @name = name
  @conversion_fn = conversion_fn
  @conversion_fn_code = conversion_fn_code

  return unless pure_conversion_fn?

  # Our conversion function is pure, so we can go ahead
  # and define it. This way, we can use this MappedObject
  # at the class-level as well as at the instance-level.
  define_singleton_method(@conversion_fn) do |value|
    @conversion_fn_code.call(value)
  end
end

Instance Attribute Details

#nameObject (readonly)


28
29
30
# File 'lib/puppet/provider/aix_object.rb', line 28

def name
  @name
end

Instance Method Details

#pure_conversion_fn?Boolean


45
46
47
# File 'lib/puppet/provider/aix_object.rb', line 45

def pure_conversion_fn?
  @conversion_fn_code.arity == 1
end

#set_provider(provider) ⇒ Object

Sets our MappedObject's provider. This only makes sense if it has an impure conversion function. We will call this in the instance-level mappings method after the provider instance has been created to define our conversion function. Note that a MappedObject with an impure conversion function cannot be used at the class level.


55
56
57
58
59
# File 'lib/puppet/provider/aix_object.rb', line 55

def set_provider(provider)
  define_singleton_method(@conversion_fn) do |value|
    @conversion_fn_code.call(provider, value)
  end
end