Class: Oddb2xml::CompareV5

Inherits:
Object
  • Object
show all
Defined in:
lib/oddb2xml/compare.rb

Constant Summary collapse

DEFAULTS =
{
  :components => ["PRODUCTS", "LIMITATIONS", "ITEMS",],
  :fields_to_ignore => ['COMP', 'DOSAGE_FORMF', 'MEASUREF'],
  :fields_as_floats => [ 'PEXT', 'PEXF', 'PPUB' ],
  :min_diff_for_floats => 0.01,
}

Instance Method Summary collapse

Constructor Details

#initialize(left, right, options = DEFAULTS.clone) ⇒ CompareV5

Returns a new instance of CompareV5.



63
64
65
66
67
68
69
70
# File 'lib/oddb2xml/compare.rb', line 63

def initialize(left, right, options = DEFAULTS.clone)
  @options = options
  @left = StammXML.new(left, @options[:components])
  @right = StammXML.new(right, @options[:components])
  @diff_stat = {}
  @occurrences = {}
  @report = []
end

Instance Method Details

#compareObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/oddb2xml/compare.rb', line 77

def compare
  show_header("Start comparing #{@left.filename} with #{@right.filename}")
  (@left.components & @right.components).each do |name|
    begin
      puts "\n#{Time.now.strftime("%H:%M:%S")}: Comparing #{name} in #{@left.basename} with #{@right.basename}"
      key = StammXML.get_component_key_name(name)
      left_items = @left.get_items(name)
      next unless left_items
      right_items = @right.get_items(name)
      next unless right_items
      @diff_stat[name] = {}
      @occurrences[name] = {}
      @diff_stat[name][NR_COMPARED] = 0
      l_names = get_names(left_items)
      r_names = get_names(right_items)
      compare_names = l_names & r_names
      l_keys = get_keys(left_items, key)
      r_keys = get_keys(right_items, key)
      (l_keys & r_keys).each do |id|
        compare_details(name, compare_names, id)
      end
      key_results_details(name, compare_names, l_keys, r_keys)
    rescue => error
      puts "Execution failed with #{error}"
    end
  end
  show_header("Summary comparing #{@left.filename} with #{@right.filename}")
  puts "Ignored differences in #{@options[:fields_to_ignore]}. Signaled when differences in #{@options[:fields_as_floats]} were bigger than #{@options[:min_diff_for_floats]}"
  puts @report.join("\n")
  @diff_stat.each do |component, stats|
    puts "\nFor #{stats[NR_COMPARED]} #{component} we have the following number of differences per field"
    stats.each do |name, nr|
      next if name.eql?(NR_COMPARED)
      next if @options[:fields_to_ignore].index(name)
      puts "   #{name.ljust(20)} #{nr} of #{@occurrences[component][name]}"
    end
  end
  @diff_stat
rescue => error
  puts "Execution failed with #{error}"
  raise error
end

#get_keys(items, key = 'GTIN') ⇒ Object



71
72
73
# File 'lib/oddb2xml/compare.rb', line 71

def get_keys(items, key='GTIN')
  items.collect{|item| item[key].first.to_i }
end

#get_names(items) ⇒ Object



74
75
76
# File 'lib/oddb2xml/compare.rb', line 74

def get_names(items)
   items.collect{|item| item.keys}.flatten.uniq.sort
end