-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Extract Bulkrax::FactoryClassFinder
This refactor introduces consolidating logic for determining an entry's factory_class. The goal is to begin to allow for us to have a CSV record that says "model = Work" and to use a "WorkResource". Note, there are downstream implementations that overwrite `factory_class` and we'll need to consider how we approach that.
- Loading branch information
Showing
11 changed files
with
93 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bulkrax | ||
class FactoryClassFinder | ||
## | ||
# @param entry [Bulkrax::Entry] | ||
# @return [Class] | ||
def self.find(entry:) | ||
new(entry: entry).find | ||
end | ||
|
||
def initialize(entry:) | ||
@entry = entry | ||
end | ||
attr_reader :entry | ||
|
||
## | ||
# @return [Class] when we are able to derive the class based on the {#name}. | ||
# @return [Nil] when we encounter errors with constantizing the {#name}. | ||
# @see #name | ||
def find | ||
# TODO: We have a string, now we want to consider how we coerce. Let's say we have Work and | ||
# WorkResource in our upstream application. Work extends ActiveFedora::Base and is legacy. | ||
# And WorkResource extends Valkyrie::Resource and is where we want to be moving. We may want | ||
# to coerce the "Work" name into "WorkResource" | ||
name.constantize | ||
rescue NameError | ||
nil | ||
rescue | ||
entry.default_work_type.constantize | ||
end | ||
|
||
## | ||
# @api private | ||
# @return [String] | ||
def name | ||
fc = if entry.parsed_metadata&.[]('model').present? | ||
Array.wrap(entry.parsed_metadata['model']).first | ||
elsif entry.importerexporter&.mapping&.[]('work_type').present? | ||
# Because of delegation's nil guard, we're reaching rather far into the implementation | ||
# details. | ||
Array.wrap(entry.parsed_metadata['work_type']).first | ||
else | ||
# The string might be frozen, so lets duplicate | ||
entry.default_work_type.dup | ||
end | ||
|
||
# Let's coerce this into the right shape. | ||
fc.tr!(' ', '_') | ||
fc.downcase! if fc.match?(/[-_]/) | ||
fc.camelcase | ||
rescue | ||
entry.default_work_type | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
module Bulkrax | ||
RSpec.describe RdfFileSetEntry, type: :model do | ||
describe '#default_work_type' do | ||
subject { described_class.new.default_work_type } | ||
it { is_expected.to eq("::FileSet") } | ||
end | ||
|
||
describe '#factory_class' do | ||
subject { described_class.new.factory_class } | ||
it { is_expected.to eq(::FileSet) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters