Class: Puppet::TransObject

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/vendor/puppet/transportable.rb

Overview

The transportable objects themselves. Basically just a hash with some metadata and a few extra methods. I used to have the object actually be a subclass of Hash, but I could never correctly dump them using YAML.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type) ⇒ TransObject

Returns a new instance of TransObject.



25
26
27
28
29
30
# File 'lib/vendor/puppet/transportable.rb', line 25

def initialize(name,type)
  @type = type.to_s.downcase
  @name = name
  @params = {}
  @tags = []
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



11
12
13
# File 'lib/vendor/puppet/transportable.rb', line 11

def catalog
  @catalog
end

#fileObject

Returns the value of attribute file.



11
12
13
# File 'lib/vendor/puppet/transportable.rb', line 11

def file
  @file
end

#lineObject

Returns the value of attribute line.



11
12
13
# File 'lib/vendor/puppet/transportable.rb', line 11

def line
  @line
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/vendor/puppet/transportable.rb', line 11

def name
  @name
end

#tagsObject



41
42
43
# File 'lib/vendor/puppet/transportable.rb', line 41

def tags
  @tags
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/vendor/puppet/transportable.rb', line 11

def type
  @type
end

Instance Method Details

#eachObject



21
22
23
# File 'lib/vendor/puppet/transportable.rb', line 21

def each
  @params.each { |p,v| yield p, v }
end

#longnameObject



32
33
34
# File 'lib/vendor/puppet/transportable.rb', line 32

def longname
  [@type,@name].join('--')
end

#refObject



36
37
38
39
# File 'lib/vendor/puppet/transportable.rb', line 36

def ref
  @ref ||= Puppet::Resource.new(@type, @name)
  @ref.to_s
end

#to_componentObject

Convert a defined type into a component.



46
47
48
49
50
51
52
53
54
55
# File 'lib/vendor/puppet/transportable.rb', line 46

def to_component
  trans = TransObject.new(ref, :component)
  @params.each { |param,value|
    next unless Puppet::Type::Component.valid_parameter?(param)
    Puppet.debug "Defining #{param} on #{ref}"
    trans[param] = value
  }
  trans.catalog = self.catalog
  Puppet::Type::Component.create(trans)
end

#to_hashObject



57
58
59
# File 'lib/vendor/puppet/transportable.rb', line 57

def to_hash
  @params.dup
end

#to_manifestObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vendor/puppet/transportable.rb', line 65

def to_manifest
  "%s { '%s':\n%s\n}" % [self.type.to_s, self.name,
    @params.collect { |p, v|
      if v.is_a? Array
        "    #{p} => [\'#{v.join("','")}\']"
      else
        "    #{p} => \'#{v}\'"
      end
    }.join(",\n")
    ]
end

#to_ralObject



93
94
95
# File 'lib/vendor/puppet/transportable.rb', line 93

def to_ral
  to_resource.to_ral
end

#to_refObject



89
90
91
# File 'lib/vendor/puppet/transportable.rb', line 89

def to_ref
  ref
end

#to_resourceObject

Create a normalized resource from our TransObject.



78
79
80
81
82
83
# File 'lib/vendor/puppet/transportable.rb', line 78

def to_resource
  result = Puppet::Resource.new(type, name, :parameters => @params.dup)
  result.tag(*tags)

  result
end

#to_sObject



61
62
63
# File 'lib/vendor/puppet/transportable.rb', line 61

def to_s
  "#{@type}(#{@name}) => #{super}"
end

#to_yaml_propertiesObject



85
86
87
# File 'lib/vendor/puppet/transportable.rb', line 85

def to_yaml_properties
  instance_variables.reject { |v| %w{@ref}.include?(v) }
end