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

pulibrary / pdc_describe / b1776731-62d7-41a7-882d-6a5b4760db75

14 May 2024 12:28PM UTC coverage: 79.223% (-16.7%) from 95.9%
b1776731-62d7-41a7-882d-6a5b4760db75

push

circleci

carolyncole
Adding a submission completion page

fixes #1791

9 of 9 new or added lines in 2 files covered. (100.0%)

567 existing lines in 42 files now uncovered.

2692 of 3398 relevant lines covered (79.22%)

60.03 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
39✔
23
    @after = after
39✔
24
    @differences = {}
39✔
25
    compare_resources
39✔
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|
39✔
38
        before_value = @before.send(method_sym)
819✔
39
        after_value = @after.send(method_sym)
819✔
40
        next if before_value.to_json == after_value.to_json
819✔
41
        if before_value.is_a?(Array)
141✔
42
          compare_arrays(method_sym, before_value, after_value)
74✔
43
        elsif before_value.respond_to?(:compare_value) || after_value.respond_to?(:compare_value)
67✔
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)
67✔
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
74✔
54
      if inside_value.respond_to?(:compare_value)
74✔
55
        compare_object_arrays(method_sym)
62✔
56
      else
57
        compare_value_arrays(method_sym)
12✔
58
      end
59
    end
60

61
    def compare_values(method_sym)
1✔
62
      compare(method_sym, &:to_s)
67✔
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") }
36✔
71
    end
72

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

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