| Class | Puppet::Rails::Resource |
| In: |
lib/puppet/rails/resource.rb
|
| Parent: | ActiveRecord::Base |
# File lib/puppet/rails/resource.rb, line 54
54: def [](param)
55: return super || parameter(param)
56: end
# File lib/puppet/rails/resource.rb, line 18
18: def add_resource_tag(tag)
19: pt = Puppet::Rails::PuppetTag.find_or_create_by_name(tag, :include => :puppet_tag)
20: resource_tags.create(:puppet_tag => pt)
21: end
# File lib/puppet/rails/resource.rb, line 23
23: def file
24: if f = self.source_file
25: return f.filename
26: else
27: return nil
28: end
29: end
# File lib/puppet/rails/resource.rb, line 31
31: def file=(file)
32: self.source_file = Puppet::Rails::SourceFile.find_or_create_by_filename(file)
33: end
returns a hash of param_names.name => [param_values]
# File lib/puppet/rails/resource.rb, line 36
36: def get_params_hash(values = nil)
37: values ||= param_values.find(:all, :include => :param_name)
38: values.inject({}) do | hash, value |
39: hash[value.param_name.name] ||= []
40: hash[value.param_name.name] << value
41: hash
42: end
43: end
# File lib/puppet/rails/resource.rb, line 45
45: def get_tag_hash(tags = nil)
46: tags ||= resource_tags.find(:all, :include => :puppet_tag)
47: return tags.inject({}) do |hash, tag|
48: # We have to store the tag object, not just the tag name.
49: hash[tag.puppet_tag.name] = tag
50: hash
51: end
52: end
# File lib/puppet/rails/resource.rb, line 62
62: def parameter(param)
63: if pn = param_names.find_by_name(param)
64: if pv = param_values.find(:first, :conditions => [ 'param_name_id = ?', pn] )
65: return pv.value
66: else
67: return nil
68: end
69: end
70: end
# File lib/puppet/rails/resource.rb, line 72
72: def parameters
73: result = get_params_hash
74: result.each do |param, value|
75: if value.is_a?(Array)
76: result[param] = value.collect { |v| v.value }
77: else
78: result[param] = value.value
79: end
80: end
81: result
82: end
# File lib/puppet/rails/resource.rb, line 84
84: def ref
85: "%s[%s]" % [self[:restype].split("::").collect { |s| s.capitalize }.join("::"), self[:title]]
86: end
Convert our object to a resource. Do not retain whether the object is exported, though, since that would cause it to get stripped from the configuration.
# File lib/puppet/rails/resource.rb, line 91
91: def to_resource(scope)
92: hash = self.attributes
93: hash["type"] = hash["restype"]
94: hash.delete("restype")
95:
96: # FIXME At some point, we're going to want to retain this information
97: # for logging and auditing.
98: hash.delete("host_id")
99: hash.delete("updated_at")
100: hash.delete("source_file_id")
101: hash.delete("created_at")
102: hash.delete("id")
103: hash.each do |p, v|
104: hash.delete(p) if v.nil?
105: end
106: hash[:scope] = scope
107: hash[:source] = scope.source
108: hash[:params] = []
109: names = []
110: self.param_names.each do |pname|
111: # We can get the same name multiple times because of how the
112: # db layout works.
113: next if names.include?(pname.name)
114: names << pname.name
115: hash[:params] << pname.to_resourceparam(self, scope.source)
116: end
117: obj = Puppet::Parser::Resource.new(hash)
118:
119: # Store the ID, so we can check if we're re-collecting the same resource.
120: obj.rails_id = self.id
121:
122: return obj
123: end