Commit bc65fac558505ac0b854d2374c129f7e21b119ba

Authored by Carlos Morais + Eduardo Morais
Committed by Carlos Morais
1 parent e03ad5db

[mezuro] Put attribute xsi:type on xml when needed

plugins/mezuro/lib/kalibro/entities/entity.rb
... ... @@ -2,7 +2,7 @@ class Kalibro::Entities::Entity
2 2  
3 3 def self.from_hash(hash)
4 4 entity = self.new
5   - hash.each { |field, value| entity.set(field, value) }
  5 + hash.each { |field, value| entity.set(field, value) if field != :attributes!}
6 6 entity
7 7 end
8 8  
... ... @@ -24,22 +24,16 @@ class Kalibro::Entities::Entity
24 24 fields.each do |field|
25 25 field_value = self.get(field)
26 26 hash[field] = convert_to_hash(field_value) if ! field_value.nil?
27   -# TODO
28   -# if field_value is object
29   -# hash[:attributes!] += {field.to_sym =>
30   -# {'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
31   -# 'xsi:type' => 'kalibro:' + field_value_type + 'Xml' } }
32   -# end
  27 + if need_xml_type?(field_value)
  28 + hash = {:attributes! => {}}.merge(hash)
  29 + hash[:attributes!][field.to_sym] = {
  30 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  31 + 'xsi:type' => 'kalibro:' + xml_class_name(field_value) }
  32 + end
33 33 end
34 34 hash
35 35 end
36 36  
37   - def convert_to_hash(value)
38   - return value.collect { |element| convert_to_hash(element) } if value.kind_of?(Array)
39   - return value.to_hash if value.kind_of?(Kalibro::Entities::Entity)
40   - value
41   - end
42   -
43 37 def ==(other)
44 38 begin
45 39 fields.each.inject(true) { |equal, field| equal && (self.get(field) == other.get(field)) }
... ... @@ -48,6 +42,8 @@ class Kalibro::Entities::Entity
48 42 end
49 43 end
50 44  
  45 + protected
  46 +
51 47 def fields
52 48 instance_variable_names.each.collect { |variable| variable.to_s.sub(/@/, '').to_sym }
53 49 end
... ... @@ -56,4 +52,21 @@ class Kalibro::Entities::Entity
56 52 send("#{field}")
57 53 end
58 54  
  55 + def convert_to_hash(value)
  56 + return value.collect { |element| convert_to_hash(element) } if value.kind_of?(Array)
  57 + return value.to_hash if value.kind_of?(Kalibro::Entities::Entity)
  58 + value
  59 + end
  60 +
  61 + def need_xml_type?(value)
  62 + value.is_a?(Kalibro::Entities::Entity) and value.class.superclass != Kalibro::Entities::Entity
  63 + end
  64 +
  65 + def xml_class_name(entity)
  66 + xml_name = entity.class.name
  67 + xml_name["Kalibro::Entities::"] = ""
  68 + xml_name[0..0] = xml_name[0..0].downcase
  69 + xml_name + "Xml"
  70 + end
  71 +
59 72 end
... ...
plugins/mezuro/test/fixtures/compound_metric_with_error_fixtures.rb
... ... @@ -10,7 +10,10 @@ class CompoundMetricWithErrorFixtures
10 10 end
11 11  
12 12 def self.create_hash
13   - {:metric => CompoundMetricFixtures.sc_hash, :error => ErrorFixtures.create_hash}
  13 + {:metric => CompoundMetricFixtures.sc_hash, :error => ErrorFixtures.create_hash,
  14 + :attributes! => {:metric => {
  15 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  16 + 'xsi:type' => 'kalibro:compoundMetricXml' }}}
14 17 end
15 18  
16 19 end
... ...
plugins/mezuro/test/fixtures/metric_configuration_fixtures.rb
... ... @@ -25,12 +25,18 @@ class MetricConfigurationFixtures
25 25  
26 26 def self.amloc_configuration_hash
27 27 {:metric => NativeMetricFixtures.amloc_hash, :code => 'amloc', :weight => 1.0,
28   - :aggregation_form => 'AVERAGE', :range =>
29   - [RangeFixtures.amloc_excellent_hash, RangeFixtures.amloc_bad_hash]}
  28 + :aggregation_form => 'AVERAGE',
  29 + :range => [RangeFixtures.amloc_excellent_hash, RangeFixtures.amloc_bad_hash],
  30 + :attributes! => {:metric => {
  31 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  32 + 'xsi:type' => 'kalibro:nativeMetricXml' }}}
30 33 end
31 34  
32 35 def self.sc_configuration_hash
33   - {:metric => CompoundMetricFixtures.sc_hash, :code => 'sc', :weight => 1.0, :aggregation_form => 'AVERAGE'}
  36 + {:metric => CompoundMetricFixtures.sc_hash, :code => 'sc', :weight => 1.0, :aggregation_form => 'AVERAGE',
  37 + :attributes! => {:metric => {
  38 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  39 + 'xsi:type' => 'kalibro:compoundMetricXml' }}}
34 40 end
35 41  
36 42 end
... ...
plugins/mezuro/test/fixtures/metric_result_fixtures.rb
... ... @@ -23,11 +23,17 @@ class MetricResultFixtures
23 23  
24 24 def self.amloc_result_hash
25 25 {:metric => NativeMetricFixtures.amloc_hash, :value => 0.0, :descendent_result => [40.0, 42.0],
26   - :range => RangeFixtures.amloc_excellent_hash}
  26 + :range => RangeFixtures.amloc_excellent_hash,
  27 + :attributes! => {:metric => {
  28 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  29 + 'xsi:type' => 'kalibro:nativeMetricXml' }}}
27 30 end
28 31  
29 32 def self.sc_result_hash
30   - {:metric => CompoundMetricFixtures.sc_hash, :value => 1.0, :descendent_result => [2.0, 42.0]}
  33 + {:metric => CompoundMetricFixtures.sc_hash, :value => 1.0, :descendent_result => [2.0, 42.0],
  34 + :attributes! => {:metric => {
  35 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  36 + 'xsi:type' => 'kalibro:compoundMetricXml' }}}
31 37 end
32 38  
33 39 end
... ...