Skip to content

Commit

Permalink
Merge pull request #122 from collectionspace/cache-stuff
Browse files Browse the repository at this point in the history
Use cache provided by collectionspace-csv-importer
  • Loading branch information
kspurgin authored Oct 13, 2021
2 parents 92bf74e + cf6193b commit d05248f
Show file tree
Hide file tree
Showing 188 changed files with 7,933 additions and 1,388 deletions.
313 changes: 1 addition & 312 deletions .reek.yml

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
AllCops:
NewCops: enable
SuggestExtensions:
rubocop-rake: false
Layout/SpaceBeforeBlockBraces:
EnforcedStyle: no_space
EnforcedStyleForEmptyBraces: no_space
Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space
Lint/RedundantCopDisableDirective:
Enabled: false
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ 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
- nothing

## [2.5.1] - 2021-10-13
### Changed
- accept and handle collectionspace-refcache passed in from collectionspace-csv-importer
- bug fixes for dealing with cached data
- refactoring

## [2.5.0] - 2021-09-23
### Added
- `multiple_recs_found` batch configuration option added to allow batch deletion of duplicate records. This defaults to `fail`, which means if there are two or more existing records sharing the same ID, the batch importer will not transfer anything for that ID. In rare cases, however, you may really need to delete duplicates, and now you can. The batch importer will transfer your update or delete to the first result found via a search for the record ID. See [the batch configuration options documentation](https://github.com/collectionspace/collectionspace-mapper/blob/main/doc/batch_configuration.adoc) for more information.
Expand Down
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github){|repo_name| "https://github.com/#{repo_name}" }

ruby '2.7.4'

Expand Down
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'collectionspace/mapper'
Expand Down
14 changes: 11 additions & 3 deletions collectionspace-mapper.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'collectionspace/mapper/version'
Expand Down Expand Up @@ -29,20 +31,26 @@ Gem::Specification.new do |spec|

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
`git ls-files -z`.split("\x0").reject{ |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.executables = spec.files.grep(%r{^exe/}){ |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'chronic'
spec.add_dependency 'facets'
spec.add_dependency 'memo_wise', '~> 1.1.0'
spec.add_dependency 'nokogiri', '>= 1.10.9'
spec.add_dependency 'xxhash', '>= 0.4.0'

spec.add_development_dependency 'bundler', '>= 2.1.2'
spec.add_development_dependency 'pry-byebug'
spec.add_development_dependency 'rake', '>= 13.0.1'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rubocop', '~> 1.18.3'

# Uncomment these if you need to use the scripts in utils/benchmarking
#spec.add_development_dependency 'ruby-prof', '~> 1.4.3'
#spec.add_development_dependency 'time_up', '~> 0.0.7'
end
17 changes: 10 additions & 7 deletions lib/collectionspace/mapper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'collectionspace/mapper/version'
require 'collectionspace/client'
require 'collectionspace/refcache'
Expand All @@ -19,7 +21,7 @@ module Mapper
LOGGER = Logger.new(STDERR)

THE_BOMB = "\u{1F4A3}"

Dir[File.dirname(__FILE__) + 'mapper/tools/*.rb'].sort.each do |file|
require "collectionspace/mapper/tools/#{File.basename(file, File.extname(file))}"
end
Expand All @@ -31,8 +33,8 @@ module Mapper
end

module Errors
class UnprocessableDataError < StandardError
UnprocessableDataError = CollectionSpace::Mapper::Errors::UnprocessableDataError
class UnprocessableDataError < StandardError
UnprocessableDataError = CollectionSpace::Mapper::Errors::UnprocessableDataError
attr_reader :input
def initialize(message, input)
super(message)
Expand All @@ -47,7 +49,8 @@ def setup_data(data, config = Mapper::Config.new)
elsif data.is_a?(CollectionSpace::Mapper::Response)
response = data
else
raise Errors::UnprocessableDataError.new("Cannot process a #{data.class}. Need a Hash or Mapper::Response", data)
raise Errors::UnprocessableDataError.new("Cannot process a #{data.class}. Need a Hash or Mapper::Response",
data)
end

response.merged_data.empty? ? merge_default_values(response, config) : response
Expand All @@ -56,7 +59,7 @@ def setup_data(data, config = Mapper::Config.new)
def merge_default_values(data, batchconfig)
defaults = batchconfig.default_values
return data unless defaults

mdata = data.orig_data.clone
defaults.each do |f, val|
if batchconfig.force_defaults
Expand All @@ -69,10 +72,10 @@ def merge_default_values(data, batchconfig)
data.merged_data = mdata.compact.transform_keys(&:downcase)
data
end

def term_key(term)
"#{term[:refname].type}-#{term[:refname].subtype}-#{term[:refname].display_name}"
end

end
end
end
9 changes: 4 additions & 5 deletions lib/collectionspace/mapper/authority_hierarchy_prepper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ module Mapper
class AuthorityHierarchyPrepper < CollectionSpace::Mapper::DataPrepper
include CollectionSpace::Mapper::TermSearchable
attr_reader :errors, :warnings, :type, :subtype

def initialize(data, handler)
super
@cache = @handler.mapper.csidcache
@cache = @handler.mapper.termcache
@type = @response.merged_data['term_type']
@subtype = @response.merged_data['term_subtype']
@errors = []
@warnings = []
end

def prep
set_id
split_data
Expand All @@ -34,13 +34,12 @@ def set_id
nt = @response.merged_data['narrower_term']
@response.identifier = "#{bt} > #{nt}"
end

def process_xpaths
clear_unmapped_mappings
@handler.mapper.xpath = @handler.xpath_hash
super
end


# these mappings were needed to get data in via template for processing, but
# do not actually get used to produce XML
Expand Down
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/authority_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Mapper

# transforms authority display name into RefName
class AuthorityTransformer < Transformer

def initialize(opts)
super
@type = opts[:transform][0]
Expand Down
10 changes: 5 additions & 5 deletions lib/collectionspace/mapper/column_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module Mapper
# :reek:InstanceVariableAssumption is spurious; we are setting the instance variables here
# by iterating through the mapper hash. Given that the mapper data is created by the
# Untangler, I am trusting it will be consistent and I'm not validating that expected
# keys are present for now. This also makes writing tests onthe methods here a bit easier.
# keys are present for now. This also makes writing tests onthe methods here a bit easier.
class ColumnMapping
attr_reader :recmapper, :data_type, :fieldname, :in_repeating_group, :is_group, :namespace, :opt_list_values,
:repeats, :source_type, :transforms, :xpath
:repeats, :source_type, :transforms, :xpath
def initialize(mapping_hash, recmapper)
@recmapper = recmapper
mapping_hash.each do |key, value|
Expand All @@ -26,20 +26,20 @@ def fullpath
@fullpath ||= [@namespace, @xpath].flatten.join('/')
end

# includes both truly required and "required in template"
# includes both truly required and "required in template"
def required?
@required.start_with?('y')
end

def update_transforms(new_transforms)
@transforms = @transforms.merge(new_transforms)
end

private

def symbolize_transforms
return if @transforms.blank?

@transforms.transform_keys!(&:to_sym)
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/collectionspace/mapper/column_mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ColumnMappings

attr_reader :config
def_delegators :@all, :each, :length, :map, :reject!, :select

def initialize(opts = {})
@mapper = opts[:mapper]
@config = @mapper.config
Expand Down Expand Up @@ -45,6 +45,7 @@ def required_columns

def service_type=(mawdule)
return unless mawdule

extend(mawdule)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/column_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.create(column:, value:, recmapper:, mapping:)
MultivalColumnValue.new(column: column, value: value, recmapper: recmapper, mapping: mapping)
when 2
GroupColumnValue.new(column: column, value: value, recmapper: recmapper, mapping: mapping)
when 3 #bonsai conservation fertilizerToBeUsed is the only field like this
when 3 # bonsai conservation fertilizerToBeUsed is the only field like this
GroupMultivalColumnValue.new(column: column, value: value, recmapper: recmapper, mapping: mapping)
when 4
SubgroupColumnValue.new(column: column, value: value, recmapper: recmapper, mapping: mapping)
Expand Down
16 changes: 9 additions & 7 deletions lib/collectionspace/mapper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ module Mapper
# or non-hierarchichal relationships via module extension
# :reek:InstanceVariableAssumption - instance variables are set during initialization
class Config
attr_reader :delimiter, :subgroup_delimiter, :response_mode, :multiple_recs_found, :force_defaults, :check_record_status,
:check_terms, :date_format, :two_digit_year_handling, :transforms, :default_values,
attr_reader :delimiter, :subgroup_delimiter, :response_mode, :multiple_recs_found, :force_defaults,
:check_record_status, :check_terms, :date_format, :two_digit_year_handling, :transforms, :default_values,
:record_type
# todo: move default config in here
include Tools::Symbolizable

DEFAULT_CONFIG = { delimiter: '|',
DEFAULT_CONFIG = {delimiter: '|',
subgroup_delimiter: '^^',
response_mode: 'normal',
multiple_recs_found: 'fail',
Expand All @@ -41,7 +41,7 @@ def initialize(opts = {})
self.record_type = opts[:record_type]

@default_values = {}

if config.is_a?(String)
set_instance_variables(JSON.parse(config))
elsif config.is_a?(Hash)
Expand All @@ -60,7 +60,7 @@ def hash
config = symbolize(config)
transforms = config[:transforms]
return config unless transforms

config[:transforms] = symbolize_transforms(transforms)
config
end
Expand All @@ -74,13 +74,15 @@ def add_default_value(column, value)

def record_type=(mawdule)
return unless mawdule

extend(mawdule)
end

def to_h
hash = {}
instance_variables.each do |var|
next if var == :@record_type

key = var.to_s.delete('@').to_sym
hash[key] = instance_variable_get(var)
end
Expand All @@ -90,7 +92,7 @@ def to_h
def set_instance_variables(hash)
hash.each{ |key, value| instance_variable_set("@#{key}", value) }
end

def validate
begin
has_required_attributes
Expand All @@ -112,7 +114,7 @@ def valid_response_mode
raise ConfigResponseModeError.new("Invalid response_mode value in config: #{@response_mode}")
end
end

def has_required_attributes
required_keys = DEFAULT_CONFIG.keys
remaining_keys = required_keys - hash.keys
Expand Down
Loading

0 comments on commit d05248f

Please sign in to comment.