diff options
| author | Luke Kanies <luke@madstop.com> | 2009-04-08 16:56:41 -0500 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-04-22 14:39:36 +1000 |
| commit | 6314745c054ba9482f145b4ec798431ac2f300a3 (patch) | |
| tree | 851f037a5aad8af01949ea8e9d82c369f1c9a2b6 /lib/puppet/parser | |
| parent | be30a618272d9828f90f5e726a23021be3b23221 (diff) | |
| download | puppet-6314745c054ba9482f145b4ec798431ac2f300a3.tar.gz puppet-6314745c054ba9482f145b4ec798431ac2f300a3.tar.xz puppet-6314745c054ba9482f145b4ec798431ac2f300a3.zip | |
Refactoring the Rails integration
This moves all code from the Parser class into
the ActiveRecord classes, and gets rid of
'ar_hash_merge'.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet/parser')
| -rw-r--r-- | lib/puppet/parser/resource.rb | 80 | ||||
| -rw-r--r-- | lib/puppet/parser/resource/param.rb | 72 |
2 files changed, 6 insertions, 146 deletions
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index 8d29ea346..23df1c624 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -58,6 +58,12 @@ class Puppet::Parser::Resource @ref.builtin = bool end + def eachparam + @params.each do |name, param| + yield param + end + end + # Retrieve the associated definition and evaluate it. def evaluate if klass = @ref.definedtype @@ -167,49 +173,6 @@ class Puppet::Parser::Resource end end - # Modify this resource in the Rails database. Poor design, yo. - def modify_rails(db_resource) - args = rails_args - args.each do |param, value| - db_resource[param] = value unless db_resource[param] == value - end - - # Handle file specially - if (self.file and - (!db_resource.file or db_resource.file != self.file)) - db_resource.file = self.file - end - - updated_params = @params.reject { |name, param| param.value == :undef }.inject({}) do |hash, ary| - hash[ary[0].to_s] = ary[1] - hash - end - - db_resource.ar_hash_merge(db_resource.get_params_hash(), updated_params, - :create => Proc.new { |name, parameter| - parameter.to_rails(db_resource) - }, :delete => Proc.new { |values| - values.each { |value| Puppet::Rails::ParamValue.delete(value['id']) } - }, :modify => Proc.new { |db, mem| - mem.modify_rails_values(db) - }) - - updated_tags = tags.inject({}) { |hash, tag| - hash[tag] = tag - hash - } - - db_resource.ar_hash_merge(db_resource.get_tag_hash(), - updated_tags, - :create => Proc.new { |name, tag| - db_resource.add_resource_tag(name) - }, :delete => Proc.new { |tag| - Puppet::Rails::ResourceTag.delete(tag['id']) - }, :modify => Proc.new { |db, mem| - # nothing here - }) - end - # Return the resource name, or the title if no name # was specified. def name @@ -262,26 +225,6 @@ class Puppet::Parser::Resource end end - # Turn our parser resource into a Rails resource. - def to_rails(host) - args = rails_args - - db_resource = host.resources.build(args) - - # Handle file specially - db_resource.file = self.file - - db_resource.save - - @params.each { |name, param| - next if param.value == :undef - param.to_rails(db_resource) - } - - tags.each { |tag| db_resource.add_resource_tag(tag) } - - return db_resource - end # Create a Puppet::Resource instance from this parser resource. # We plan, at some point, on not needing to do this conversion, but @@ -426,17 +369,6 @@ class Puppet::Parser::Resource end end - def rails_args - return [:type, :title, :line, :exported].inject({}) do |hash, param| - # 'type' isn't a valid column name, so we have to use another name. - to = (param == :type) ? :restype : param - if value = self.send(param) - hash[to] = value - end - hash - end - end - # Make sure the resource's parameters are all valid for the type. def validate @params.each do |name, param| diff --git a/lib/puppet/parser/resource/param.rb b/lib/puppet/parser/resource/param.rb index 7ce58f4c4..6e22d3e17 100644 --- a/lib/puppet/parser/resource/param.rb +++ b/lib/puppet/parser/resource/param.rb @@ -22,80 +22,8 @@ class Puppet::Parser::Resource::Param def line_to_i return line ? Integer(line) : nil end - - # Make sure an array (or possibly not an array) of values is correctly - # set up for Rails. The main thing is that Resource::Reference objects - # should stay objects, so they just get serialized. - def munge_for_rails(values) - values = value.is_a?(Array) ? value : [value] - values.map do |v| - if v.is_a?(Puppet::Parser::Resource::Reference) - v - else - v.to_s - end - end - end - - # Store a new parameter in a Rails db. - def to_rails(db_resource) - values = munge_for_rails(value) - - param_name = Puppet::Rails::ParamName.find_or_create_by_name(self.name.to_s) - line_number = line_to_i() - return values.collect do |v| - db_resource.param_values.create(:value => v, - :line => line_number, - :param_name => param_name) - end - end - - def modify_rails_values(db_values) - #dev_warn if db_values.nil? || db_values.empty? - - values_to_remove(db_values).each { |remove_me| - Puppet::Rails::ParamValue.delete(remove_me['id']) - } - line_number = line_to_i() - db_param_name = db_values[0]['param_name_id'] - values_to_add(db_values).each { |add_me| - Puppet::Rails::ParamValue.create(:value => add_me, - :line => line_number, - :param_name_id => db_param_name, - :resource_id => db_values[0]['resource_id'] ) - } - end def to_s "%s => %s" % [self.name, self.value] end - - def compare(v,db_value) - if (v.is_a?(Puppet::Parser::Resource::Reference)) - return v.to_s == db_value.to_s - else - return v == db_value - end - end - - def values_to_remove(db_values) - values = munge_for_rails(value) - line_number = line_to_i() - db_values.collect do |db| - db unless (db['line'] == line_number && - values.find { |v| - compare(v,db['value']) - } ) - end.compact - end - - def values_to_add(db_values) - values = munge_for_rails(value) - line_number = line_to_i() - values.collect do |v| - v unless db_values.find { |db| (compare(v,db['value']) && - line_number == db['line']) } - end.compact - end end - |
