loc_mods 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66939abfdba314e93de07afbe306be58394fb4466de3d9203318e400b2a0f017
4
- data.tar.gz: 5e9ffbdd0c405ac026e77138c5287c34d21cf93c84a7d98e94e5d6e987b015a0
3
+ metadata.gz: 2b206d76220e77a18fd6609c3f7646eaf8fb836ac2d69a7d6da019db5820f24d
4
+ data.tar.gz: ffd23bb7508ad1fef1e0b165eb07620fc0a2420b6c436ac34cec8208affb6463
5
5
  SHA512:
6
- metadata.gz: 9e0cec87a093a33c3785376aaba4cbe7b05b3ee8e916c1fa238faa659668e01468b56ec08afc9478123a01adb0076e497e1f09a8dcdc4f330965aa6f82fddcc2
7
- data.tar.gz: f5ed1dc6d38e5c6afec2edd7872963433e1cc699502686acd6045198b4b37fec1d27720f7777c2f08a1a5f2234c15d98ccf2beefe2babfca3cf897007b7ee6c5
6
+ metadata.gz: 9f56bbb4bfdab2dd2820ef67d94461ac5caa47d62937762a5cbeacbea8e70497c75968d92b3ce23f5d7e79f6a737c98c43058d6e11b774d0777340f040808fea
7
+ data.tar.gz: 0ac7feedcebe5df178f6ad313ba811004de126c82adc0f5289f44b43212a26ef2acfa3f09a45afa9cd4b158b7b4ef97976413ac357b4eb11749ab227c801a168
data/exe/loc-mods CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require_relative "../lib/loc_mods/cli"
@@ -1,9 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # lib/loc_mods/base_mapper.rb
2
4
  require "shale"
3
5
  require_relative "comparable_mapper"
4
6
 
5
7
  module LocMods
8
+ # Base class for all object definitions
6
9
  class BaseMapper < Shale::Mapper
7
10
  include ComparableMapper
8
11
  end
12
+
13
+ # Nil class substitute for comparison
14
+ class ComparableNil < BaseMapper
15
+ end
16
+
17
+ # Comparison of two values for ComparableMapper
18
+ class Comparison
19
+ attr_accessor :original, :updated
20
+
21
+ def initialize(original:, updated:)
22
+ @original = original
23
+ @updated = updated
24
+ end
25
+ end
9
26
  end
data/lib/loc_mods/cli.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # lib/loc_mods/cli.rb
2
4
  require "thor"
3
5
  require "loc_mods"
@@ -23,33 +25,34 @@ module LocMods
23
25
  records_by_url = {}
24
26
  all_records.each do |record|
25
27
  urls = record[:record].location.flat_map { |loc| loc.url.map(&:content) }.compact
26
- if urls.any?
27
- urls.each do |url|
28
- records_by_url[url] ||= []
29
- records_by_url[url] << record
30
- end
31
- else
28
+ unless urls.any?
32
29
  puts "Warning: Record without URL found in file: #{record[:file]}"
30
+ next
31
+ end
32
+
33
+ urls.each do |url|
34
+ records_by_url[url] ||= []
35
+ records_by_url[url] << record
33
36
  end
34
37
  end
35
38
 
36
39
  duplicate_count = 0
37
40
  records_by_url.each do |url, records|
38
- if records.size > 1
39
- duplicate_count += 1
40
- puts "Duplicate set ##{duplicate_count} found for URL: #{url}"
41
- records.combination(2).each_with_index do |(record1, record2), index|
42
- puts " Comparison #{index + 1}:"
43
- puts " File 1: #{record1[:file]}"
44
- puts " File 2: #{record2[:file]}"
45
- differences = record1[:record].compare(record2[:record])
46
- if differences
47
- puts " ----"
48
- print_differences(differences)
49
- puts " ----"
50
- end
51
- puts "\n"
41
+ next unless records.size > 1
42
+
43
+ duplicate_count += 1
44
+ puts "Duplicate set ##{duplicate_count} found for URL: #{url}"
45
+ records.combination(2).each_with_index do |(record1, record2), index|
46
+ puts " Comparison #{index + 1}:"
47
+ puts " File 1: #{record1[:file]}"
48
+ puts " File 2: #{record2[:file]}"
49
+ differences = record1[:record].compare(record2[:record])
50
+ if differences
51
+ puts " ----"
52
+ print_differences(differences)
53
+ puts " ----"
52
54
  end
55
+ puts "\n"
53
56
  end
54
57
  end
55
58
  end
@@ -65,26 +68,68 @@ module LocMods
65
68
  end
66
69
 
67
70
  def print_differences(differences, prefix = "", path = [])
71
+ if differences.is_a?(Comparison)
72
+ print_difference(differences, path)
73
+ return
74
+ end
75
+
76
+ # Differences is a Hash here
68
77
  differences.each do |key, value|
78
+ # puts "key #{key}, value #{value}"
69
79
  current_path = path + [key]
70
- if value.is_a?(Hash) && value.keys.all? { |k| k.is_a?(Integer) }
71
- value.each do |index, sub_value|
72
- print_differences(sub_value, prefix, current_path + [index])
80
+ # This is a comparison
81
+ if value.is_a?(Comparison)
82
+ print_difference(value, path)
83
+ next
84
+ end
85
+
86
+ raise "Differences must be in form of a Hash" unless value.is_a?(Hash)
87
+
88
+ # This is not array, end here
89
+ next unless value.keys.any? do |k|
90
+ k.is_a?(Integer) || k == :_array_size_difference
91
+ end
92
+
93
+ # if value[:_array_size_difference]
94
+ # puts " #{format_path(current_path)} [_array_size_difference]:"
95
+ # puts " Record 1: #{format_value(value[:_array_size_difference].original)}"
96
+ # puts " Record 2: #{format_value(value[:_array_size_difference].updated)}"
97
+ # puts
98
+ # end
99
+
100
+ value.each do |subkey, subvalue|
101
+ # if [:self, :other].include?(subkey)
102
+ # raise "Subkey is self or other"
103
+ # end
104
+ # puts "subkey (#{subkey})"
105
+ # puts "subvalue #{subvalue}, prefix #{prefix}, current_path + [subkey] #{current_path + [subkey]}"
106
+
107
+ if subkey.is_a?(Integer)
108
+ print_differences(subvalue, prefix, current_path + [subkey])
109
+ else
110
+ if subvalue.is_a?(Comparison)
111
+ print_difference(subvalue, current_path + [subkey])
112
+ next
113
+ end
114
+
115
+ raise "In an array diff but not an Integer!"
73
116
  end
74
- elsif value.is_a?(Hash) && (value[:self] || value[:other])
75
- puts " #{format_path(current_path)}:"
76
- puts " #{prefix} Record 1: #{format_value(value[:self])}"
77
- puts " #{prefix} Record 2: #{format_value(value[:other])}"
78
- puts
79
- elsif value.is_a?(Hash)
80
- print_differences(value, prefix, current_path)
81
117
  end
82
118
  end
83
119
  end
84
120
 
121
+ def print_difference(value, current_path)
122
+ return unless value.original || value.updated
123
+
124
+ puts " #{format_path(current_path)}:"
125
+ puts " Record 1: #{format_value(value.original)}"
126
+ puts " Record 2: #{format_value(value.updated)}"
127
+ puts
128
+ end
129
+
85
130
  def format_path(path)
86
131
  path.map.with_index do |part, index|
87
- if index == 0
132
+ if index.zero?
88
133
  part.to_s
89
134
  elsif part.is_a?(Integer)
90
135
  "[#{part}]"
@@ -95,7 +140,14 @@ module LocMods
95
140
  end
96
141
 
97
142
  def format_value(value)
98
- value.nil? ? "(nil)" : "\"#{value}\""
143
+ case value
144
+ when nil, ComparableNil
145
+ "(nil)"
146
+ when String
147
+ "\"#{value}\""
148
+ else
149
+ value.to_s
150
+ end
99
151
  end
100
152
  end
101
153
  end
@@ -1,30 +1,42 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # lib/loc_mods/comparable_mapper.rb
2
4
 
3
5
  module LocMods
6
+ # Enable comparison of two class models solely based on attribute values in
7
+ # a recursive manner.
4
8
  module ComparableMapper
5
- # def self.included(base)
6
- # base.extend(ClassMethods)
9
+ # TODO: Implement Comparable
10
+ # include Comparable
11
+ # def <=>(other)
12
+ # attributes.foo <=> other.attributes.foo
7
13
  # end
8
-
9
- # module ClassMethods
10
- # def attribute_mapping
11
- # @attribute_mapping ||= attributes.to_h do |name, attribute|
12
- # [name, attribute.type]
13
- # end
14
- # end
14
+ # def inspect
15
+ # @foo
15
16
  # end
16
17
 
17
- def compare(other)
18
- return {} unless other.is_a?(self.class)
18
+ def eql?(other)
19
+ other.class == self.class &&
20
+ self.class.attributes.all? do |attr|
21
+ send(attr) == other.send(attr)
22
+ end
23
+ end
19
24
 
25
+ alias == eql?
26
+
27
+ def hash
28
+ ([self.class] + self.class.attributes.map(&:hash)).hash
29
+ end
30
+
31
+ def compare(other)
20
32
  differences = {}
21
33
 
22
34
  # puts "Debugging: Attributes for #{self.class.name}"
23
35
  # pp self.class.attributes.keys
24
36
 
25
37
  self.class.attributes.each_key do |attr|
26
- self_value = self.send(attr)
27
- other_value = other.send(attr)
38
+ self_value = respond_to?(attr) ? send(attr) : nil
39
+ other_value = other.respond_to?(attr) ? other.send(attr) : nil
28
40
 
29
41
  # puts "Debugging: Comparing attribute '#{attr}'"
30
42
  # puts " Self value: #{self_value.inspect}"
@@ -52,14 +64,14 @@ module LocMods
52
64
  case self_value
53
65
  when Array
54
66
  # puts "compare_values case 1"
55
- compare_arrays(self_value, other_value)
56
- when Shale::Mapper
67
+ compare_arrays(self_value, other_value || [])
68
+ when ComparableMapper
57
69
  # puts "compare_values case 2"
58
70
  self_value.compare(other_value)
59
71
  else
60
72
  if self_value != other_value
61
73
  # puts "compare_values case 3"
62
- { self: self_value, other: other_value }
74
+ Comparison.new(original: self_value, updated: other_value)
63
75
  end
64
76
  end
65
77
  end
@@ -69,25 +81,27 @@ module LocMods
69
81
  max_length = [self_array.size, other_array.size].max
70
82
 
71
83
  max_length.times do |index|
72
- self_item = self_array[index]
73
- other_item = other_array[index]
74
-
75
- if index >= self_array.size
76
- compared = compare_values(other_item, nil)
77
- differences[index] = { self: compared[:other], other: compared[:self] }
78
- elsif index >= other_array.size
79
- differences[index] = compare_values(self_item, nil)
80
- else
81
- compared = compare_values(self_item, other_item)
82
- differences[index] = compared if compared
83
- end
84
+ self_item = self_array[index] || ComparableNil.new
85
+ other_item = other_array[index] || ComparableNil.new
86
+
87
+ compared = compare_values(self_item, other_item)
88
+ differences[index] = compared if compared
89
+
90
+ # if index >= self_array.size
91
+ # puts "case 1 #{compared}"
92
+ # # differences[index] = compared
93
+ # elsif index >= other_array.size
94
+ # puts "case 2.1 #{self_item}"
95
+ # puts "case 2.2 #{compared}"
96
+ # # differences[index] = compared
97
+ # end
84
98
  end
85
99
 
86
100
  if self_array.size != other_array.size
87
- differences[:array_size_difference] = {
88
- self: self_array.size,
89
- other: other_array.size,
90
- }
101
+ differences[:_array_size_difference] = Comparison.new(
102
+ original: self_array.size,
103
+ updated: other_array.size
104
+ )
91
105
  end
92
106
 
93
107
  differences.empty? ? nil : differences
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LocMods
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loc_mods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-13 00:00:00.000000000 Z
11
+ date: 2024-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri