• 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

84.55
/app/models/pdc_metadata/resource.rb
1
# frozen_string_literal: true
2
module PDCMetadata
1✔
3
  # Represents a PUL Datacite resource
4
  # https://support.datacite.org/docs/datacite-metadata-schema-v44-properties-overview
5

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

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

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

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

48
    def individual_contributors
1✔
49
      @contributors
2✔
50
    end
51

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

56
    def identifier
1✔
57
      @doi
×
58
    end
59

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

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

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

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

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

83
    class << self
1✔
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
87
      def new_from_jsonb(jsonb_hash)
1✔
88
        resource = PDCMetadata::Resource.new
2✔
89
        return resource if jsonb_hash.blank?
2✔
90
        set_basics(resource, jsonb_hash)
2✔
91
        set_curator_controlled_metadata(resource, jsonb_hash)
2✔
92
        set_additional_metadata(resource, jsonb_hash)
2✔
93
        set_titles(resource, jsonb_hash)
2✔
94
        set_creators(resource, jsonb_hash)
2✔
95
        set_individual_contributors(resource, jsonb_hash)
2✔
96
        set_organizational_contributors(resource, jsonb_hash)
2✔
97
        set_related_objects(resource, jsonb_hash)
2✔
98
        set_funders(resource, jsonb_hash)
2✔
99

100
        resource
2✔
101
      end
102

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

107
      def default_resource_type_general
1✔
108
        "Dataset"
2✔
109
      end
110

111
      private
1✔
112

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

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

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

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

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

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

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

158
        def set_titles(resource, hash)
1✔
159
          titles = hash["titles"] || []
2✔
160

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

166
        def set_related_objects(resource, hash)
1✔
167
          related_objects = hash["related_objects"] || []
2✔
168

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

179
        def set_creators(resource, hash)
1✔
180
          creators = hash["creators"] || []
2✔
181

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

188
        def set_individual_contributors(resource, hash)
1✔
189
          individual_contributors = hash["contributors"] || []
2✔
190

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

197
        def set_organizational_contributors(resource, hash)
1✔
198
          organizational_contributors = hash["organizational_contributors"] || []
2✔
199

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

205
        def set_funders(resource, hash)
1✔
206
          funders = hash["funders"] || []
2✔
207

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