diff --git a/plugins/mezuro/lib/kalibro/entities/entity.rb b/plugins/mezuro/lib/kalibro/entities/entity.rb index f617296..fd17565 100644 --- a/plugins/mezuro/lib/kalibro/entities/entity.rb +++ b/plugins/mezuro/lib/kalibro/entities/entity.rb @@ -2,7 +2,7 @@ class Kalibro::Entities::Entity def self.from_hash(hash) entity = self.new - hash.each { |field, value| entity.set(field, value) } + hash.each { |field, value| entity.set(field, value) if field != :attributes!} entity end @@ -24,22 +24,16 @@ class Kalibro::Entities::Entity fields.each do |field| field_value = self.get(field) hash[field] = convert_to_hash(field_value) if ! field_value.nil? -# TODO -# if field_value is object -# hash[:attributes!] += {field.to_sym => -# {'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', -# 'xsi:type' => 'kalibro:' + field_value_type + 'Xml' } } -# end + if need_xml_type?(field_value) + hash = {:attributes! => {}}.merge(hash) + hash[:attributes!][field.to_sym] = { + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', + 'xsi:type' => 'kalibro:' + xml_class_name(field_value) } + end end hash end - def convert_to_hash(value) - return value.collect { |element| convert_to_hash(element) } if value.kind_of?(Array) - return value.to_hash if value.kind_of?(Kalibro::Entities::Entity) - value - end - def ==(other) begin fields.each.inject(true) { |equal, field| equal && (self.get(field) == other.get(field)) } @@ -48,6 +42,8 @@ class Kalibro::Entities::Entity end end + protected + def fields instance_variable_names.each.collect { |variable| variable.to_s.sub(/@/, '').to_sym } end @@ -56,4 +52,21 @@ class Kalibro::Entities::Entity send("#{field}") end + def convert_to_hash(value) + return value.collect { |element| convert_to_hash(element) } if value.kind_of?(Array) + return value.to_hash if value.kind_of?(Kalibro::Entities::Entity) + value + end + + def need_xml_type?(value) + value.is_a?(Kalibro::Entities::Entity) and value.class.superclass != Kalibro::Entities::Entity + end + + def xml_class_name(entity) + xml_name = entity.class.name + xml_name["Kalibro::Entities::"] = "" + xml_name[0..0] = xml_name[0..0].downcase + xml_name + "Xml" + end + end diff --git a/plugins/mezuro/test/fixtures/compound_metric_with_error_fixtures.rb b/plugins/mezuro/test/fixtures/compound_metric_with_error_fixtures.rb index f7084de..0a64f7c 100644 --- a/plugins/mezuro/test/fixtures/compound_metric_with_error_fixtures.rb +++ b/plugins/mezuro/test/fixtures/compound_metric_with_error_fixtures.rb @@ -10,7 +10,10 @@ class CompoundMetricWithErrorFixtures end def self.create_hash - {:metric => CompoundMetricFixtures.sc_hash, :error => ErrorFixtures.create_hash} + {:metric => CompoundMetricFixtures.sc_hash, :error => ErrorFixtures.create_hash, + :attributes! => {:metric => { + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', + 'xsi:type' => 'kalibro:compoundMetricXml' }}} end end diff --git a/plugins/mezuro/test/fixtures/metric_configuration_fixtures.rb b/plugins/mezuro/test/fixtures/metric_configuration_fixtures.rb index 3c9512f..a5bc752 100644 --- a/plugins/mezuro/test/fixtures/metric_configuration_fixtures.rb +++ b/plugins/mezuro/test/fixtures/metric_configuration_fixtures.rb @@ -25,12 +25,18 @@ class MetricConfigurationFixtures def self.amloc_configuration_hash {:metric => NativeMetricFixtures.amloc_hash, :code => 'amloc', :weight => 1.0, - :aggregation_form => 'AVERAGE', :range => - [RangeFixtures.amloc_excellent_hash, RangeFixtures.amloc_bad_hash]} + :aggregation_form => 'AVERAGE', + :range => [RangeFixtures.amloc_excellent_hash, RangeFixtures.amloc_bad_hash], + :attributes! => {:metric => { + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', + 'xsi:type' => 'kalibro:nativeMetricXml' }}} end def self.sc_configuration_hash - {:metric => CompoundMetricFixtures.sc_hash, :code => 'sc', :weight => 1.0, :aggregation_form => 'AVERAGE'} + {:metric => CompoundMetricFixtures.sc_hash, :code => 'sc', :weight => 1.0, :aggregation_form => 'AVERAGE', + :attributes! => {:metric => { + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', + 'xsi:type' => 'kalibro:compoundMetricXml' }}} end end diff --git a/plugins/mezuro/test/fixtures/metric_result_fixtures.rb b/plugins/mezuro/test/fixtures/metric_result_fixtures.rb index 2352c62..854880b 100644 --- a/plugins/mezuro/test/fixtures/metric_result_fixtures.rb +++ b/plugins/mezuro/test/fixtures/metric_result_fixtures.rb @@ -23,11 +23,17 @@ class MetricResultFixtures def self.amloc_result_hash {:metric => NativeMetricFixtures.amloc_hash, :value => 0.0, :descendent_result => [40.0, 42.0], - :range => RangeFixtures.amloc_excellent_hash} + :range => RangeFixtures.amloc_excellent_hash, + :attributes! => {:metric => { + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', + 'xsi:type' => 'kalibro:nativeMetricXml' }}} end def self.sc_result_hash - {:metric => CompoundMetricFixtures.sc_hash, :value => 1.0, :descendent_result => [2.0, 42.0]} + {:metric => CompoundMetricFixtures.sc_hash, :value => 1.0, :descendent_result => [2.0, 42.0], + :attributes! => {:metric => { + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', + 'xsi:type' => 'kalibro:compoundMetricXml' }}} end end -- libgit2 0.21.2