Skip to content

Commit

Permalink
Merge pull request #140 from collectionspace/eager-load
Browse files Browse the repository at this point in the history
Eager load to play nicely with collectionspace-csv-importer
  • Loading branch information
kspurgin authored Jun 7, 2022
2 parents a79c700 + 2ba8849 commit dca7ffb
Show file tree
Hide file tree
Showing 45 changed files with 116 additions and 112 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ This project bumps the version number for any changes (including documentation u
## [Unreleased] - i.e. pushed to main branch but not yet tagged as a release
- none

## [4.0.2] - 2022-06-07
Uses Zeitwerk eager loading (and custom inflection on version.rb to VERSION) to make `rails:zeitwerk` check pass when used with collectionspace-csv-importer.

## [4.0.1] - 2022-05-23

See [PR 138](https://github.com/collectionspace/collectionspace-mapper/pull/138) for more details on changes.
Expand Down
15 changes: 8 additions & 7 deletions lib/collectionspace/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
require 'xxhash'
require 'zeitwerk'

loader = Zeitwerk::Loader.new
loader.inflector.inflect(
"version" => "VERSION"
)
loader.push_dir("#{__dir__}/mapper", namespace: CollectionSpace::Mapper)
loader.setup
loader.eager_load

module CollectionSpace
::CS = CollectionSpace
module Mapper


LOGGER = Logger.new(STDERR)

THE_BOMB = "\u{1F4A3}"
Expand Down Expand Up @@ -68,8 +73,4 @@ def merge_default_values(data, batchconfig)
data
end
end

loader = Zeitwerk::Loader.new
loader.push_dir("#{__dir__}/mapper", namespace: CollectionSpace::Mapper)
loader.setup
end
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/column_mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def required_columns
attr_reader :mapper, :config, :all, :lkup

def add_mapping(mapping_hash)
mapobj = CS::Mapper::ColumnMapping.new(mapping_hash, mapper)
mapobj = CollectionSpace::Mapper::ColumnMapping.new(mapping_hash, mapper)
all << mapobj
lkup[mapobj.datacolumn] = mapobj
end
Expand Down
8 changes: 4 additions & 4 deletions lib/collectionspace/mapper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ def has_required_attributes
def record_type_extension
case record_type
when 'media'
CS::Mapper::Media
CollectionSpace::Mapper::Media
when 'objecthierarchy'
CS::Mapper::ObjectHierarchy
CollectionSpace::Mapper::ObjectHierarchy
when 'authorityhierarchy'
CS::Mapper::AuthorityHierarchy
CollectionSpace::Mapper::AuthorityHierarchy
when 'nonhierarchicalrelationship'
CS::Mapper::NonHierarchicalRelationship
CollectionSpace::Mapper::NonHierarchicalRelationship
else
nil
end
Expand Down
6 changes: 3 additions & 3 deletions lib/collectionspace/mapper/data_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def initialize(record_mapper:, client:, cache:, csid_cache:, config: {})
@mapper = CollectionSpace::Mapper::RecordMapper.new(mapper: record_mapper, batchconfig: config,
csclient: client, termcache: cache,
csidcache: csid_cache )
@validator = CS::Mapper::DataValidator.new(@mapper, @mapper.termcache)
@searcher = CS::Mapper::Searcher.new(client: client, config: mapper.batchconfig)
@date_handler = CS::Mapper::Dates::StructuredDateHandler.new(
@validator = CollectionSpace::Mapper::DataValidator.new(@mapper, @mapper.termcache)
@searcher = CollectionSpace::Mapper::Searcher.new(client: client, config: mapper.batchconfig)
@date_handler = CollectionSpace::Mapper::Dates::StructuredDateHandler.new(
client: client,
cache: cache,
csid_cache: csid_cache,
Expand Down
4 changes: 2 additions & 2 deletions lib/collectionspace/mapper/dates/chronic_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ module CollectionSpace
module Mapper
module Dates
class ChronicParser
include CS::Mapper::Dates::Mappable
include CollectionSpace::Mapper::Dates::Mappable

attr_reader :mappable
def initialize(date_string, handler)
@date_string = date_string
@handler = handler
parsed = parse
return CS::Mapper::Dates::ServicesParser.new(date_string, handler) unless parsed
return CollectionSpace::Mapper::Dates::ServicesParser.new(date_string, handler) unless parsed

@mappable = map_timestamp(parsed)
self
Expand Down
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/dates/services_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module CollectionSpace
module Mapper
module Dates
class ServicesParser
include CS::Mapper::Dates::Mappable
include CollectionSpace::Mapper::Dates::Mappable

attr_reader :mappable
def initialize(date_string, handler)
Expand Down
16 changes: 8 additions & 8 deletions lib/collectionspace/mapper/dates/structured_date_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ def ce

def call(date_string)
if date_string == '%NULLVALUE%'
CS::Mapper::Dates::NullDate.new
CollectionSpace::Mapper::Dates::NullDate.new
elsif date_string == THE_BOMB
CS::Mapper::Dates::DateBomber.new
CollectionSpace::Mapper::Dates::DateBomber.new
elsif date_formats.any?{ |re| date_string.match?(re) }
CS::Mapper::Dates::ChronicParser.new(date_string, self)
CollectionSpace::Mapper::Dates::ChronicParser.new(date_string, self)
elsif two_digit_year_date_formats.any?{ |re| date_string.match?(re) }
CS::Mapper::Dates::TwoDigitYearHandler.new(date_string, self)
CollectionSpace::Mapper::Dates::TwoDigitYearHandler.new(date_string, self)
elsif service_parseable_month_formats.any?{ |re| date_string.match?(re) }
CS::Mapper::Dates::ServicesParser.new(date_string, self)
CollectionSpace::Mapper::Dates::ServicesParser.new(date_string, self)
elsif other_month_formats.any?{ |re| date_string.match?(re) }
CS::Mapper::Dates::YearMonthDateCreator.new(date_string, self)
CollectionSpace::Mapper::Dates::YearMonthDateCreator.new(date_string, self)
elsif date_string.match?(/^\d{4}$/)
CS::Mapper::Dates::YearDateCreator.new(date_string, self)
CollectionSpace::Mapper::Dates::YearDateCreator.new(date_string, self)
else
CS::Mapper::Dates::ServicesParser.new(date_string, self)
CollectionSpace::Mapper::Dates::ServicesParser.new(date_string, self)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/collectionspace/mapper/dates/two_digit_year_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module CollectionSpace
module Mapper
module Dates
class TwoDigitYearHandler
include CS::Mapper::Dates::Mappable
include CollectionSpace::Mapper::Dates::Mappable

attr_reader :mappable

Expand All @@ -13,9 +13,9 @@ def initialize(date_string, handler)
@year_handling = handler.config.two_digit_year_handling

if literal?
@mappable = CS::Mapper::Dates::ServicesParser.new(date_string, handler).mappable
@mappable = CollectionSpace::Mapper::Dates::ServicesParser.new(date_string, handler).mappable
elsif coerce?
mappable = CS::Mapper::Dates::ChronicParser.new(coerced_year_date, handler).mappable
mappable = CollectionSpace::Mapper::Dates::ChronicParser.new(coerced_year_date, handler).mappable
mappable['dateDisplayDate'] = date_string
@mappable = mappable
else
Expand Down
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/dates/year_date_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module CollectionSpace
module Mapper
module Dates
class YearDateCreator
include CS::Mapper::Dates::Mappable
include CollectionSpace::Mapper::Dates::Mappable

attr_reader :mappable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module CollectionSpace
module Mapper
module Dates
class YearMonthDateCreator
include CS::Mapper::Dates::Mappable
include CollectionSpace::Mapper::Dates::Mappable

attr_reader :mappable

Expand Down
14 changes: 7 additions & 7 deletions lib/collectionspace/mapper/record_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RecordMapper
def initialize(opts)
jhash = opts[:mapper].is_a?(Hash) ? opts[:mapper] : JSON.parse(opts[:mapper])
convert(jhash)
@batchconfig = CS::Mapper::Config.new(config: opts[:batchconfig], record_type: record_type)
@batchconfig = CollectionSpace::Mapper::Config.new(config: opts[:batchconfig], record_type: record_type)
@csclient = opts[:csclient]
@termcache = opts[:termcache]
@csidcache = opts[:csidcache]
Expand All @@ -38,21 +38,21 @@ def record_type
def service_type_extension
case config.service_type
when 'authority'
CS::Mapper::Authority
CollectionSpace::Mapper::Authority
when 'relation'
CS::Mapper::Relationship
CollectionSpace::Mapper::Relationship
when 'procedure'
CS::Mapper::Media if record_type == 'media'
CollectionSpace::Mapper::Media if record_type == 'media'
end
end

private

def convert(json)
hash = symbolize(json)
@config = CS::Mapper::RecordMapperConfig.new(hash[:config])
@xml_template = CS::Mapper::XmlTemplate.new(hash[:docstructure])
@mappings = CS::Mapper::ColumnMappings.new(mappings: hash[:mappings],
@config = CollectionSpace::Mapper::RecordMapperConfig.new(hash[:config])
@xml_template = CollectionSpace::Mapper::XmlTemplate.new(hash[:docstructure])
@mappings = CollectionSpace::Mapper::ColumnMappings.new(mappings: hash[:mappings],
mapper: self)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/row_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RowData
def initialize(datahash, recmapper)
@recmapper = recmapper
@columns = datahash.map do |column, value|
CS::Mapper::ColumnValue.create(column: column,
CollectionSpace::Mapper::ColumnValue.create(column: column,
value: value,
recmapper: @recmapper,
mapping: @recmapper.mappings.lookup(column))
Expand Down
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/searcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module CollectionSpace
module Mapper
class Searcher
def initialize(client:, config: CS::Mapper::Config.new({}))
def initialize(client:, config: CollectionSpace::Mapper::Config.new({}))
@client = client
@active = config.search_if_not_cached
@search_fields = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class RecordStatusServiceBuilder
class << self
def call(client, mapper)
if mapper.batchconfig.status_check_method == 'client'
CS::Mapper::Tools::RecordStatusServiceClient.new(client, mapper)
CollectionSpace::Mapper::Tools::RecordStatusServiceClient.new(client, mapper)
else
CS::Mapper::Tools::RecordStatusServiceCache.new(mapper)
CollectionSpace::Mapper::Tools::RecordStatusServiceCache.new(mapper)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Tools
# Returns status of records, based on presence in CSID Cache.
# @note This should **only** be used with expert migration tooling with which you can
# confidently ensure the CSID Cache is accurately populated with all existing records
class RecordStatusServiceCache < CS::Mapper::Tools::RecordStatusServiceBuilder
class RecordStatusServiceCache < CollectionSpace::Mapper::Tools::RecordStatusServiceBuilder
def initialize(mapper)
super
@refname_cache = mapper.termcache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module CollectionSpace
module Mapper
module Tools
class RecordStatusServiceClient < CS::Mapper::Tools::RecordStatusServiceBuilder
class RecordStatusServiceClient < CollectionSpace::Mapper::Tools::RecordStatusServiceBuilder
def initialize(client, mapper)
super
@client = client
Expand Down Expand Up @@ -32,14 +32,14 @@ def get_service
subtype: subtype
)
rescue KeyError
raise CS::Mapper::NoClientServiceError,
raise CollectionSpace::Mapper::NoClientServiceError,
"#{type} > #{subtype}"
end
else
begin
client.service(type: type)
rescue KeyError
raise CS::Mapper::NoClientServiceError, type
raise CollectionSpace::Mapper::NoClientServiceError, type
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/collectionspace/mapper/unknown_term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module CollectionSpace
module Mapper
class UnknownTerm

class ReconstituteNilError < CS::Mapper::Error
class ReconstituteNilError < CollectionSpace::Mapper::Error
def initialize
msg = "Cannot reconstitute from NilValue"
super(msg)
Expand All @@ -27,7 +27,7 @@ def initialize(type:, subtype:, term:)
@subtype = subtype
@display_name = term
@identifier = term
# Named "urn" to be used the same way as CS::Mapper::Tools::RefName
# Named "urn" to be used the same way as CollectionSpace::Mapper::Tools::RefName
# This value gets cached and can be reconstituted into an UnknownTerm object
@urn = "#{type}|||#{subtype}|||#{term}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module CollectionSpace
module Mapper
VERSION = '4.0.1'
VERSION = '4.0.2'
end
end
2 changes: 1 addition & 1 deletion spec/collectionspace/mapper/column_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# :opt_list_values=>[],
# :datacolumn=>"numberValue",
# :required=>"n"
let(:recordmapper){ instance_double('CS::Mapper::RecordMapper') }
let(:recordmapper){ instance_double('CollectionSpace::Mapper::RecordMapper') }
let(:mapping){ described_class.new(hash, recordmapper) }

describe '#datacolumn' do
Expand Down
8 changes: 4 additions & 4 deletions spec/collectionspace/mapper/column_mappings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
]
end

let(:recordmapper){ instance_double('CS::Mapper::RecordMapper') }
let(:mapperconfig){ instance_double('CS::Mapper::RecordMapperConfig') }
let(:recordmapper){ instance_double('CollectionSpace::Mapper::RecordMapper') }
let(:mapperconfig){ instance_double('CollectionSpace::Mapper::RecordMapperConfig') }

let(:mappingsobj){ dc = described_class.new(mappings: mappings, mapper: recordmapper) }

Expand All @@ -84,7 +84,7 @@
context 'when initialized from authority RecordMapper' do
it 'adds shortIdentifier to mappings' do
allow(mapperconfig).to receive(:common_namespace).and_return('citations_common')
allow(recordmapper).to receive(:service_type_extension).and_return(CS::Mapper::Authority)
allow(recordmapper).to receive(:service_type_extension).and_return(CollectionSpace::Mapper::Authority)
authmappings = described_class.new(mappings: mappings,
mapper: recordmapper)
expect(authmappings.known_columns.include?('shortidentifier')).to be true
Expand Down Expand Up @@ -129,7 +129,7 @@

it 'adds mediaFileURI to mappings' do
allow(mapperconfig).to receive(:common_namespace).and_return('media_common')
allow(recordmapper).to receive(:service_type_extension).and_return(CS::Mapper::Media)
allow(recordmapper).to receive(:service_type_extension).and_return(CollectionSpace::Mapper::Media)

mediamappings = described_class.new(mappings: mappings,
mapper: recordmapper)
Expand Down
10 changes: 5 additions & 5 deletions spec/collectionspace/mapper/column_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@
let(:colname){ 'collection' }
let(:colvalue){ 'blah' }
it 'returns ColumnValue' do
expect(creator).to be_a(CS::Mapper::ColumnValue)
expect(creator).to be_a(CollectionSpace::Mapper::ColumnValue)
end
end

context 'given core collectionobject comment value' do
let(:colname){ 'comment' }
let(:colvalue){ 'blah' }
it 'returns MultivalColumnValue' do
expect(creator).to be_a(CS::Mapper::MultivalColumnValue)
expect(creator).to be_a(CollectionSpace::Mapper::MultivalColumnValue)
end
end

context 'given core collectionobject comment value' do
let(:colname){ 'title' }
let(:colvalue){ 'blah' }
it 'returns GroupColumnValue' do
expect(creator).to be_a(CS::Mapper::GroupColumnValue)
expect(creator).to be_a(CollectionSpace::Mapper::GroupColumnValue)
end
end

Expand All @@ -50,15 +50,15 @@
let(:colname){ 'fertilizerToBeUsed' }
let(:colvalue){ 'blah' }
it 'returns GroupMultivalColumnValue' do
expect(creator).to be_a(CS::Mapper::GroupMultivalColumnValue)
expect(creator).to be_a(CollectionSpace::Mapper::GroupMultivalColumnValue)
end
end

context 'given core collectionobject titleTranslation value' do
let(:colname){ 'titleTranslation' }
let(:colvalue){ 'blah' }
it 'returns SubgroupColumnValue' do
expect(creator).to be_a(CS::Mapper::SubgroupColumnValue)
expect(creator).to be_a(CollectionSpace::Mapper::SubgroupColumnValue)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/collectionspace/mapper/data_quality_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'spec_helper'

RSpec.describe CollectionSpace::Mapper::DataQualityChecker do
let(:recordmapper){ instance_double('CS::Mapper::RecordMapper') }
let(:recordmapper){ instance_double('CollectionSpace::Mapper::RecordMapper') }
let(:mapping){ CollectionSpace::Mapper::ColumnMapping.new(maphash, recordmapper) }

context 'when source_type = optionlist' do
Expand Down
Loading

0 comments on commit dca7ffb

Please sign in to comment.