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

pulibrary / pdc_describe / 4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

07 Apr 2025 06:36PM UTC coverage: 1.283% (-94.6%) from 95.862%
4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

Pull #1994

circleci

hectorcorrea
Switched to use the autocomplete that we aleady use for ROR. Integrated it with the existing logic for creators
Pull Request #1994: Started adding auto complete to contributors

0 of 46 new or added lines in 2 files covered. (0.0%)

4806 existing lines in 74 files now uncovered.

65 of 5065 relevant lines covered (1.28%)

0.01 hits per line

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

0.0
/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

UNCOV
18
class ResourceCompareService
×
UNCOV
19
  attr_reader :differences
×
20

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

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

UNCOV
32
  private
×
33

UNCOV
34
    def compare_resources
×
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.
UNCOV
37
      @before.accessors.each do |method_sym|
×
UNCOV
38
        before_value = @before.send(method_sym)
×
UNCOV
39
        after_value = @after.send(method_sym)
×
UNCOV
40
        next if before_value.to_json == after_value.to_json
×
UNCOV
41
        if before_value.is_a?(Array)
×
UNCOV
42
          compare_arrays(method_sym, before_value, after_value)
×
UNCOV
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.
UNCOV
45
          compare_objects(method_sym)
×
UNCOV
46
        else
×
UNCOV
47
          compare_values(method_sym)
×
UNCOV
48
        end
×
UNCOV
49
      end
×
UNCOV
50
    end
×
51

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

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

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

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

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

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