Commit c9898c9f34f2211f5ec314a4e7855038d3716566

Authored by Alessandro Palmeira + Caio Salgado
Committed by Paulo Meireles
1 parent 35236463

[Mezuro] Modified to_hash to accept options and fixed test

plugins/mezuro/lib/kalibro/metric_configuration.rb
... ... @@ -51,7 +51,7 @@ class Kalibro::MetricConfiguration < Kalibro::Model
51 51 def save
52 52 begin
53 53 self.class.request("MetricConfiguration", :save_metric_configuration, {
54   - :metric_configuration => to_hash,
  54 + :metric_configuration => to_hash(:except => [:configuration_name]),
55 55 :configuration_name => configuration_name})
56 56 true
57 57 rescue Exception => error
... ... @@ -66,22 +66,6 @@ class Kalibro::MetricConfiguration < Kalibro::Model
66 66 })
67 67 end
68 68  
69   - def to_hash
70   - hash = Hash.new
71   - fields.each do |field|
72   - if (field != :configuration_name)
73   - field_value = send(field)
74   - hash[field] = convert_to_hash(field_value)
75   - if field_value.is_a?(Kalibro::Model)
76   - hash = {:attributes! => {}}.merge(hash)
77   - hash[:attributes!][field.to_sym] = {
78   - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
79   - 'xsi:type' => 'kalibro:' + xml_class_name(field_value) }
80   - end
81   - end
82   - end
83   - hash
84   - end
85 69  
86 70 private
87 71  
... ...
plugins/mezuro/lib/kalibro/model.rb
... ... @@ -4,16 +4,19 @@ class Kalibro::Model
4 4 attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
5 5 end
6 6  
7   - def to_hash
  7 + def to_hash(options={})
8 8 hash = Hash.new
  9 + excepts = !options[:except].nil? ? options[:except] : []
9 10 fields.each do |field|
10   - field_value = send(field)
11   - hash[field] = convert_to_hash(field_value) if ! field_value.nil?
12   - if field_value.is_a?(Kalibro::Model)
13   - hash = {:attributes! => {}}.merge(hash)
14   - hash[:attributes!][field.to_sym] = {
15   - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
16   - 'xsi:type' => 'kalibro:' + xml_class_name(field_value) }
  11 + if(!excepts.include?(field))
  12 + field_value = send(field)
  13 + hash[field] = convert_to_hash(field_value) if ! field_value.nil?
  14 + if field_value.is_a?(Kalibro::Model)
  15 + hash = {:attributes! => {}}.merge(hash)
  16 + hash[:attributes!][field.to_sym] = {
  17 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  18 + 'xsi:type' => 'kalibro:' + xml_class_name(field_value) }
  19 + end
17 20 end
18 21 end
19 22 hash
... ...
plugins/mezuro/test/unit/kalibro/metric_configuration_test.rb
... ... @@ -19,7 +19,7 @@ class MetricConfigurationTest < ActiveSupport::TestCase
19 19 end
20 20  
21 21 should 'convert metric configuration to hash' do
22   - assert_equal @native_metric_configuration_hash, @native_metric_configuration_hash
  22 + assert_equal @native_metric_configuration_hash, @native_metric_configuration.to_hash(:except => [:configuration_name])
23 23 end
24 24  
25 25 should 'create appropriate metric type' do
... ...