• 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/models/pdc_metadata/resource.rb
1
# frozen_string_literal: true
UNCOV
2
module PDCMetadata
×
3
  # Represents a PUL Datacite resource
4
  # https://support.datacite.org/docs/datacite-metadata-schema-v44-properties-overview
5

UNCOV
6
  def self.fuzzy_match(obj, value)
×
UNCOV
7
    obj.key.to_s == value or obj.value.casecmp(value).zero?
×
UNCOV
8
  end
×
9

10
  # rubocop:disable Metrics/ClassLength
UNCOV
11
  class Resource
×
UNCOV
12
    attr_accessor :creators, :titles, :publisher, :publication_year, :resource_type, :resource_type_general,
×
UNCOV
13
      :description, :doi, :ark, :rights_many, :version_number, :collection_tags, :keywords, :related_objects,
×
UNCOV
14
      :funders, :organizational_contributors, :domains, :migrated, :communities, :subcommunities
×
15

16
    # rubocop:disable Metrics/MethodLength
UNCOV
17
    def initialize(doi: nil, title: nil, resource_type: nil, resource_type_general: nil, creators: [], description: nil)
×
UNCOV
18
      @titles = []
×
UNCOV
19
      @titles << PDCMetadata::Title.new(title:) unless title.nil?
×
UNCOV
20
      @description = description
×
UNCOV
21
      @collection_tags = []
×
UNCOV
22
      @creators = creators
×
UNCOV
23
      @resource_type = resource_type || "Dataset"
×
UNCOV
24
      @resource_type_general = resource_type_general || self.class.default_resource_type_general
×
UNCOV
25
      @publisher = "Princeton University"
×
UNCOV
26
      @publication_year = Time.zone.today.year
×
UNCOV
27
      @ark = nil
×
UNCOV
28
      @doi = doi
×
UNCOV
29
      @rights_many = []
×
UNCOV
30
      @version_number = "1"
×
UNCOV
31
      @related_objects = []
×
UNCOV
32
      @keywords = []
×
UNCOV
33
      @contributors = []
×
UNCOV
34
      @organizational_contributors = []
×
UNCOV
35
      @funders = []
×
UNCOV
36
      @domains = []
×
UNCOV
37
      @communities = []
×
UNCOV
38
      @subcommunities = []
×
UNCOV
39
      @migrated = false
×
UNCOV
40
    end
×
41
    # rubocop:enable Metrics/MethodLength
42

UNCOV
43
    def accessors
×
UNCOV
44
      setters = methods.map(&:to_s).filter { |s| s.match?(/\w=$/) }
×
UNCOV
45
      setters.map { |s| s.delete("=").to_sym }
×
UNCOV
46
    end
×
47

UNCOV
48
    def individual_contributors
×
UNCOV
49
      @contributors
×
UNCOV
50
    end
×
51

UNCOV
52
    def individual_contributors=(value)
×
UNCOV
53
      @contributors = value
×
UNCOV
54
    end
×
55

UNCOV
56
    def identifier
×
UNCOV
57
      @doi
×
UNCOV
58
    end
×
59

UNCOV
60
    def identifier_type
×
UNCOV
61
      return nil if @doi.nil?
×
UNCOV
62
      "DOI"
×
UNCOV
63
    end
×
64

UNCOV
65
    def main_title
×
UNCOV
66
      @titles.find(&:main?)&.title
×
UNCOV
67
    end
×
68

UNCOV
69
    def other_titles
×
UNCOV
70
      @titles.select { |title| title.main? == false }
×
UNCOV
71
    end
×
72

UNCOV
73
    def to_xml
×
UNCOV
74
      xml_declaration = '<?xml version="1.0"?>'
×
UNCOV
75
      xml_body = datacite_serialization.to_xml
×
UNCOV
76
      xml_declaration + "\n" + xml_body + "\n"
×
UNCOV
77
    end
×
78

UNCOV
79
    def datacite_serialization
×
UNCOV
80
      @datacite_serialization ||= PDCSerialization::Datacite.new_from_work_resource(self)
×
UNCOV
81
    end
×
82

UNCOV
83
    class << self
×
84
      # Creates a PDCMetadata::Resource from a JSONB postgres field
85
      #  This jsonb_hash can be created by running JSON.parse(pdc_metadata_resource.to_json)
86
      #   or by loading it from the work.metadata jsonb field
UNCOV
87
      def new_from_jsonb(jsonb_hash)
×
UNCOV
88
        resource = PDCMetadata::Resource.new
×
UNCOV
89
        return resource if jsonb_hash.blank?
×
UNCOV
90
        set_basics(resource, jsonb_hash)
×
UNCOV
91
        set_curator_controlled_metadata(resource, jsonb_hash)
×
UNCOV
92
        set_additional_metadata(resource, jsonb_hash)
×
UNCOV
93
        set_titles(resource, jsonb_hash)
×
UNCOV
94
        set_creators(resource, jsonb_hash)
×
UNCOV
95
        set_individual_contributors(resource, jsonb_hash)
×
UNCOV
96
        set_organizational_contributors(resource, jsonb_hash)
×
UNCOV
97
        set_related_objects(resource, jsonb_hash)
×
UNCOV
98
        set_funders(resource, jsonb_hash)
×
99

UNCOV
100
        resource
×
UNCOV
101
      end
×
102

UNCOV
103
      def resource_type_general_values
×
UNCOV
104
        Datacite::Mapping::ResourceTypeGeneral.map(&:value)
×
UNCOV
105
      end
×
106

UNCOV
107
      def default_resource_type_general
×
UNCOV
108
        "Dataset"
×
UNCOV
109
      end
×
110

UNCOV
111
      private
×
112

UNCOV
113
        def rights(rights_info)
×
UNCOV
114
          return if rights_info.nil?
×
UNCOV
115
          PDCMetadata::Rights.find(rights_info["identifier"])
×
UNCOV
116
        end
×
117

UNCOV
118
        def set_basics(resource, hash)
×
UNCOV
119
          resource.description = hash["description"]
×
UNCOV
120
          resource.publisher = hash["publisher"]
×
UNCOV
121
          resource.publication_year = hash["publication_year"]
×
122

UNCOV
123
          resource.rights_many = (hash["rights_many"] || []).map { |rights_info| rights(rights_info) }.compact
×
UNCOV
124
          if resource.rights_many.count == 0 && hash["rights"]
×
125
            # Special case to handle works created with a single-value `rights` field
126
            # instead of the new multi-value `rights_many` field.
UNCOV
127
            resource.rights_many = [rights(hash["rights"])].compact
×
UNCOV
128
          end
×
129

UNCOV
130
          resource.domains = hash["domains"] || []
×
UNCOV
131
          resource.migrated = hash["migrated"] || false
×
UNCOV
132
        end
×
133

UNCOV
134
        def set_curator_controlled_metadata(resource, hash)
×
UNCOV
135
          resource.doi = hash["doi"]
×
UNCOV
136
          resource.ark = hash["ark"]
×
UNCOV
137
          resource.version_number = hash["version_number"]
×
UNCOV
138
          resource.collection_tags = hash["collection_tags"] || []
×
UNCOV
139
          resource.resource_type = hash["resource_type"]
×
140

141
          # TODO: Older records have a different format.
142
          # When we migrate these, then this can be removed.
UNCOV
143
          resource_type_general = hash["resource_type_general"]
×
UNCOV
144
          unless resource_type_general.blank? || Datacite::Mapping::ResourceTypeGeneral.find_by_value(resource_type_general)
×
UNCOV
145
            resource_type_general = ::Datacite::Mapping::ResourceTypeGeneral.find do |obj|
×
UNCOV
146
              ::PDCMetadata.fuzzy_match(obj, resource_type_general)
×
UNCOV
147
            end.value
×
UNCOV
148
          end
×
UNCOV
149
          resource.resource_type_general = resource_type_general
×
UNCOV
150
        end
×
151

UNCOV
152
        def set_additional_metadata(resource, hash)
×
UNCOV
153
          resource.keywords = hash["keywords"] || []
×
UNCOV
154
          resource.communities = hash["communities"] || []
×
UNCOV
155
          resource.subcommunities = hash["subcommunities"] || []
×
UNCOV
156
        end
×
157

UNCOV
158
        def set_titles(resource, hash)
×
UNCOV
159
          titles = hash["titles"] || []
×
160

UNCOV
161
          titles.each do |title|
×
UNCOV
162
            resource.titles << PDCMetadata::Title.new(title: title["title"], title_type: title["title_type"])
×
UNCOV
163
          end
×
UNCOV
164
        end
×
165

UNCOV
166
        def set_related_objects(resource, hash)
×
UNCOV
167
          related_objects = hash["related_objects"] || []
×
168

UNCOV
169
          related_objects.each do |related_object|
×
UNCOV
170
            next if related_object["related_identifier"].blank? && related_object["related_identifier_type"].blank?
×
UNCOV
171
            resource.related_objects << PDCMetadata::RelatedObject.new(
×
UNCOV
172
                                          related_identifier: related_object["related_identifier"],
×
UNCOV
173
                                          related_identifier_type: related_object["related_identifier_type"],
×
UNCOV
174
                                          relation_type: related_object["relation_type"]
×
UNCOV
175
                                        )
×
UNCOV
176
          end
×
UNCOV
177
        end
×
178

UNCOV
179
        def set_creators(resource, hash)
×
UNCOV
180
          creators = hash["creators"] || []
×
181

UNCOV
182
          creators.each do |creator|
×
UNCOV
183
            resource.creators << Creator.from_hash(creator)
×
UNCOV
184
          end
×
UNCOV
185
          resource.creators.sort_by!(&:sequence)
×
UNCOV
186
        end
×
187

UNCOV
188
        def set_individual_contributors(resource, hash)
×
UNCOV
189
          individual_contributors = hash["contributors"] || []
×
190

UNCOV
191
          individual_contributors.each do |contributor|
×
UNCOV
192
            resource.individual_contributors << Creator.individual_contributor_from_hash(contributor)
×
UNCOV
193
          end
×
UNCOV
194
          resource.individual_contributors.sort_by!(&:sequence)
×
UNCOV
195
        end
×
196

UNCOV
197
        def set_organizational_contributors(resource, hash)
×
UNCOV
198
          organizational_contributors = hash["organizational_contributors"] || []
×
199

UNCOV
200
          organizational_contributors.each do |contributor|
×
UNCOV
201
            resource.organizational_contributors << Creator.organizational_contributor_from_hash(contributor)
×
UNCOV
202
          end
×
UNCOV
203
        end
×
204

UNCOV
205
        def set_funders(resource, hash)
×
UNCOV
206
          funders = hash["funders"] || []
×
207

UNCOV
208
          funders.each do |funder|
×
UNCOV
209
            resource.funders << Funder.funder_from_hash(funder)
×
UNCOV
210
          end
×
211
          # TODO: Make funders reorderable
212
          # resource.funders.sort_by!(&:sequence)
UNCOV
213
        end
×
UNCOV
214
    end
×
UNCOV
215
  end
×
216
  # rubocop:enable Metrics/ClassLength
UNCOV
217
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