diff options
-rw-r--r-- | lib/puppet/rails/host.rb | 4 | ||||
-rw-r--r-- | lib/puppet/rails/param_value.rb | 11 | ||||
-rw-r--r-- | lib/puppet/rails/resource.rb | 12 | ||||
-rwxr-xr-x | spec/unit/rails/param_value.rb | 49 | ||||
-rwxr-xr-x | spec/unit/rails/resource.rb | 42 |
5 files changed, 104 insertions, 14 deletions
diff --git a/lib/puppet/rails/host.rb b/lib/puppet/rails/host.rb index 2d00fa72b..1ddc01244 100644 --- a/lib/puppet/rails/host.rb +++ b/lib/puppet/rails/host.rb @@ -222,8 +222,8 @@ class Puppet::Rails::Host < ActiveRecord::Base accumulate_benchmark("Added resources", :parameters) { - resource.eachparam do |param| - Puppet::Rails::ParamValue.from_parser_param(param).each do |value_hash| + resource.each do |param, value| + Puppet::Rails::ParamValue.from_parser_param(param, value).each do |value_hash| db_resource.param_values.build(value_hash) end end diff --git a/lib/puppet/rails/param_value.rb b/lib/puppet/rails/param_value.rb index a5dbbaed4..46d7fc1fb 100644 --- a/lib/puppet/rails/param_value.rb +++ b/lib/puppet/rails/param_value.rb @@ -8,13 +8,12 @@ class Puppet::Rails::ParamValue < ActiveRecord::Base belongs_to :resource # Store a new parameter in a Rails db. - def self.from_parser_param(param) - values = munge_parser_values(param.value) + def self.from_parser_param(param, values) + values = munge_parser_values(values) - param_name = Puppet::Rails::ParamName.find_or_create_by_name(param.name.to_s) - line_number = param.line_to_i() + param_name = Puppet::Rails::ParamName.find_or_create_by_name(param.to_s) return values.collect do |v| - {:value => v, :line => line_number, :param_name => param_name} + {:value => v, :param_name => param_name} end end @@ -24,7 +23,7 @@ class Puppet::Rails::ParamValue < ActiveRecord::Base def self.munge_parser_values(value) values = value.is_a?(Array) ? value : [value] values.map do |v| - if v.is_a?(Puppet::Parser::Resource::Reference) + if v.is_a?(Puppet::Resource::Reference) v else v.to_s diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb index 81505cbd4..86a26ee39 100644 --- a/lib/puppet/rails/resource.rb +++ b/lib/puppet/rails/resource.rb @@ -100,8 +100,8 @@ class Puppet::Rails::Resource < ActiveRecord::Base def merge_parameters(resource) catalog_params = {} - resource.eachparam do |param| - catalog_params[param.name.to_s] = param + resource.each do |param, value| + catalog_params[param.to_s] = value end db_params = {} @@ -123,7 +123,7 @@ class Puppet::Rails::Resource < ActiveRecord::Base db_params.each do |name, value_hashes| values = value_hashes.collect { |v| v['value'] } - unless value_compare(catalog_params[name].value, values) + unless value_compare(catalog_params[name], values) value_hashes.each { |v| deletions << v['id'] } end end @@ -132,12 +132,12 @@ class Puppet::Rails::Resource < ActiveRecord::Base Puppet::Rails::ParamValue.delete(deletions) unless deletions.empty? # Lastly, add any new parameters. - catalog_params.each do |name, param| + catalog_params.each do |name, value| next if db_params.include?(name) - values = param.value.is_a?(Array) ? param.value : [param.value] + values = value.is_a?(Array) ? value : [value] values.each do |v| - param_values.build(:value => serialize_value(v), :line => param.line, :param_name => Puppet::Rails::ParamName.accumulate_by_name(name)) + param_values.build(:value => serialize_value(v), :line => resource.line, :param_name => Puppet::Rails::ParamName.accumulate_by_name(name)) end end end diff --git a/spec/unit/rails/param_value.rb b/spec/unit/rails/param_value.rb new file mode 100755 index 000000000..26e87fec2 --- /dev/null +++ b/spec/unit/rails/param_value.rb @@ -0,0 +1,49 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe "Puppet::Rails::ParamValue" do + confine "Cannot test without ActiveRecord" => Puppet.features.rails? + + def column(name, type) + ActiveRecord::ConnectionAdapters::Column.new(name, nil, type, false) + end + + before do + require 'puppet/rails/param_value' + + name = stub 'param_name', :name => "foo" + + # Stub this so we don't need access to the DB. + Puppet::Rails::ParamValue.stubs(:columns).returns([column("value", "string")]) + Puppet::Rails::ParamName.stubs(:find_or_create_by_name).returns(name) + end + + describe "when creating initial parameter values" do + it "should return an array of hashes" do + Puppet::Rails::ParamValue.from_parser_param(:myparam, %w{a b})[0].should be_instance_of(Hash) + end + + it "should return hashes for each value with the parameter name set as the ParamName instance" do + name = stub 'param_name', :name => "foo" + Puppet::Rails::ParamName.expects(:find_or_create_by_name).returns(name) + + result = Puppet::Rails::ParamValue.from_parser_param(:myparam, "a")[0] + result[:value].should == "a" + result[:param_name].should == name + end + + it "should return an array of hashes even when only one parameter is provided" do + Puppet::Rails::ParamValue.from_parser_param(:myparam, "a")[0].should be_instance_of(Hash) + end + + it "should convert all arguments into strings" do + Puppet::Rails::ParamValue.from_parser_param(:myparam, 50)[0][:value].should == "50" + end + + it "should not convert Resource References into strings" do + ref = Puppet::Resource::Reference.new(:file, "/file") + Puppet::Rails::ParamValue.from_parser_param(:myparam, ref)[0][:value].should == ref + end + end +end diff --git a/spec/unit/rails/resource.rb b/spec/unit/rails/resource.rb new file mode 100755 index 000000000..eaead2e3d --- /dev/null +++ b/spec/unit/rails/resource.rb @@ -0,0 +1,42 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +describe "Puppet::Rails::Resource" do + confine "Cannot test without ActiveRecord" => Puppet.features.rails? + + def column(name, type) + ActiveRecord::ConnectionAdapters::Column.new(name, nil, type, false) + end + + before do + require 'puppet/rails/resource' + + # Stub this so we don't need access to the DB. + Puppet::Rails::Resource.stubs(:columns).returns([column("title", "string"), column("restype", "string"), column("exported", "boolean")]) + end + + describe "when creating initial resource arguments" do + it "should set the restype to the resource's type" do + Puppet::Rails::Resource.rails_resource_initial_args(Puppet::Resource.new(:file, "/file"))[:restype].should == "File" + end + + it "should set the title to the resource's title" do + Puppet::Rails::Resource.rails_resource_initial_args(Puppet::Resource.new(:file, "/file"))[:title].should == "/file" + end + + it "should set the line to the resource's line if one is available" do + resource = Puppet::Resource.new(:file, "/file") + resource.line = 50 + + Puppet::Rails::Resource.rails_resource_initial_args(resource)[:line].should == 50 + end + + it "should set 'exported' to true of the resource is exported" do + resource = Puppet::Resource.new(:file, "/file") + resource.exported = true + + Puppet::Rails::Resource.rails_resource_initial_args(resource)[:exported].should be_true + end + end +end |