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

pulibrary / pdc_describe / 9091a1ae-29be-458c-984a-339d213919c4

12 Dec 2024 07:41PM UTC coverage: 26.434% (-69.7%) from 96.113%
9091a1ae-29be-458c-984a-339d213919c4

Pull #2000

circleci

jrgriffiniii
Removing integration with ActiveStorage
Pull Request #2000: Bump actionpack from 7.2.1.1 to 7.2.2.1

945 of 3575 relevant lines covered (26.43%)

0.35 hits per line

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

31.58
/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
×
23
    @after = after
×
24
    @differences = {}
×
25
    compare_resources
×
26
  end
27

28
  def identical?
1✔
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|
×
38
        before_value = @before.send(method_sym)
×
39
        after_value = @after.send(method_sym)
×
40
        next if before_value.to_json == after_value.to_json
×
41
        if before_value.is_a?(Array)
×
42
          compare_arrays(method_sym, before_value, after_value)
×
43
        elsif before_value.respond_to?(:compare_value) || after_value.respond_to?(:compare_value)
×
44
          # If either value is nil, we still want to get the compare_value for the other.
45
          compare_objects(method_sym)
×
46
        else
47
          compare_values(method_sym)
×
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
×
54
      if inside_value.respond_to?(:compare_value)
×
55
        compare_object_arrays(method_sym)
×
56
      else
57
        compare_value_arrays(method_sym)
×
58
      end
59
    end
60

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

65
    def compare_objects(method_sym)
1✔
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") }
×
71
    end
72

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

77
    def compare(method_sym)
1✔
78
      before_value = yield(@before.send(method_sym))
×
79
      after_value = yield(@after.send(method_sym))
×
80
      if before_value != after_value
×
81
        @differences[method_sym] = [{ action: :changed, from: before_value, to: after_value }]
×
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