• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / cace366a-ffad-45f1-9b60-678e607fa527

14 May 2024 02:21PM UTC coverage: 60.862% (-35.0%) from 95.908%
cace366a-ffad-45f1-9b60-678e607fa527

push

circleci

jrgriffiniii
wip

1 of 3 new or added lines in 2 files covered. (33.33%)

1194 existing lines in 57 files now uncovered.

2076 of 3411 relevant lines covered (60.86%)

22.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.11
/app/services/resource_compare_service.rb
1
# frozen_string_literal: true
2

3
# Compares two PDCMetadata::Resource objects and provides a hash with the `differences`
4
#
5
# If there are no differences between the two objects `identical?` returns `true` and `differences == {}`
6
# If there are differences the `differences` hash has the following structure:
7
#
8
# ```
9
#    :field_name = [{action:, from: to:, value: }]
10
# ```
11
#
12
# The array for ``:field_name` lists all the changes that happened to the field.
13
# For single-value fields is always a single element array, for multi-value fields
14
# in can contain multiple elements.
15
#
16
# The `action` indicates whether a value changed, was added, or deleted.
17

18
class ResourceCompareService
1✔
19
  attr_reader :differences
1✔
20

21
  def initialize(before, after)
1✔
22
    @before = before
10✔
23
    @after = after
10✔
24
    @differences = {}
10✔
25
    compare_resources
10✔
26
  end
27

28
  def identical?
1✔
UNCOV
29
    @differences == {}
×
30
  end
31

32
  private
1✔
33

34
    def compare_resources
1✔
35
      # Loop through an object's setters and compare the values from the before and after objects.
36
      # Note: This assumes the before and after objects have the same methods.
37
      @before.accessors.each do |method_sym|
10✔
38
        before_value = @before.send(method_sym)
210✔
39
        after_value = @after.send(method_sym)
210✔
40
        next if before_value.to_json == after_value.to_json
210✔
41
        if before_value.is_a?(Array)
27✔
42
          compare_arrays(method_sym, before_value, after_value)
20✔
43
        elsif before_value.respond_to?(:compare_value) || after_value.respond_to?(:compare_value)
7✔
44
          # If either value is nil, we still want to get the compare_value for the other.
UNCOV
45
          compare_objects(method_sym)
×
46
        else
47
          compare_values(method_sym)
7✔
48
        end
49
      end
50
    end
51

52
    def compare_arrays(method_sym, before_array, after_array)
1✔
53
      inside_value = (before_array + after_array).first
20✔
54
      if inside_value.respond_to?(:compare_value)
20✔
55
        compare_object_arrays(method_sym)
14✔
56
      else
57
        compare_value_arrays(method_sym)
6✔
58
      end
59
    end
60

61
    def compare_values(method_sym)
1✔
62
      compare(method_sym, &:to_s)
7✔
63
    end
64

65
    def compare_objects(method_sym)
1✔
UNCOV
66
      compare(method_sym) { |value| value.nil? ? "" : value.compare_value }
×
67
    end
68

69
    def compare_value_arrays(method_sym)
1✔
70
      compare(method_sym) { |values| values.join("\n") }
18✔
71
    end
72

73
    def compare_object_arrays(method_sym)
1✔
74
      compare(method_sym) { |values| values.map(&:compare_value).join("\n") }
42✔
75
    end
76

77
    def compare(method_sym)
1✔
78
      before_value = yield(@before.send(method_sym))
27✔
79
      after_value = yield(@after.send(method_sym))
27✔
80
      if before_value != after_value
27✔
81
        @differences[method_sym] = [{ action: :changed, from: before_value, to: after_value }]
25✔
82
      end
83
    end
84
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc