Skip to content

Commit

Permalink
Merge pull request #78 from collectionspace/fix-aliases
Browse files Browse the repository at this point in the history
Remove aliases to global namespace
  • Loading branch information
kspurgin authored Sep 28, 2020
2 parents 239a28f + aa0bbc2 commit f108174
Show file tree
Hide file tree
Showing 34 changed files with 213 additions and 254 deletions.
7 changes: 3 additions & 4 deletions lib/collectionspace/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

module CollectionSpace
module Mapper
::Mapper = CollectionSpace::Mapper

extend self
LOGGER = Logger.new(STDERR)
DEFAULT_CONFIG = { delimiter: ';',
subgroup_delimiter: '^^',
Expand Down Expand Up @@ -51,7 +50,7 @@ def initialize(message, input)
end
end

def self.setup_data(data)
def setup_data(data)
if data.is_a?(Hash)
Response.new(data)
elsif data.is_a?(CollectionSpace::Mapper::Response)
Expand All @@ -61,7 +60,7 @@ def self.setup_data(data)
raise Errors::UnprocessableDataError.new("Cannot process a #{data.class}. Need a Hash or Mapper::Response", data)
rescue Errors::UnprocessableDataError => error
error.set_backtrace([])
Mapper::LOGGER.error(error)
CollectionSpace::Mapper::LOGGER.error(error)
err_resp = Response.new(data)
err_resp.errors << error
err_resp
Expand Down
23 changes: 11 additions & 12 deletions lib/collectionspace/mapper/data_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ module Mapper

# given a RecordMapper hash and a data hash, returns CollectionSpace XML document
class DataHandler
::DataHandler = CollectionSpace::Mapper::DataHandler
attr_reader :mapper, :client, :cache, :config, :blankdoc, :defaults, :validator,
:is_authority, :known_fields

def initialize(record_mapper, client, cache,
config = Mapper::DEFAULT_CONFIG
config = CollectionSpace::Mapper::DEFAULT_CONFIG
)
@mapper = RecordMapper.convert(record_mapper)
@mapper = CollectionSpace::Mapper::Tools::RecordMapper.convert(record_mapper)
@client = client
@cache = cache
@config = get_config(config)
Expand All @@ -24,15 +23,15 @@ def initialize(record_mapper, client, cache,
@blankdoc = build_xml
@defaults = @config[:default_values] ? @config[:default_values].transform_keys(&:downcase) : {}
merge_config_transforms
@validator = DataValidator.new(@mapper, @cache)
@validator = CollectionSpace::Mapper::DataValidator.new(@mapper, @cache)
end

def process(data)
response = Mapper::setup_data(data)
response = CollectionSpace::Mapper::setup_data(data)
if response.valid?
prepper = DataPrepper.new(response, self)
prepper = CollectionSpace::Mapper::DataPrepper.new(response, self)
prepper.prep
mapper = DataMapper.new(prepper.response, self, prepper.xphash)
mapper = CollectionSpace::Mapper::DataMapper.new(prepper.response, self, prepper.xphash)
@response_mode == 'normal' ? mapper.response.normal : mapper.response
else
response
Expand All @@ -51,9 +50,9 @@ def validate(data)
end

def prep(data)
response = Mapper::setup_data(data)
response = CollectionSpace::Mapper::setup_data(data)
if response.valid?
prepper = DataPrepper.new(response, self)
prepper = CollectionSpace::Mapper::DataPrepper.new(response, self)
prepper.split_data
prepper.transform_data
prepper.check_data
Expand All @@ -65,14 +64,14 @@ def prep(data)
end

def map(response, xphash)
mapper = DataMapper.new(response, self, xphash)
mapper = CollectionSpace::Mapper::DataMapper.new(response, self, xphash)
@response_mode == 'normal' ? mapper.response.normal : mapper.response
end

private

def get_config(config)
config_object = Config.new(config)
config_object = CollectionSpace::Mapper::Tools::Config.new(config)
config_object.hash
end

Expand All @@ -97,7 +96,7 @@ def get_is_authority
end

# you can specify per-data-key transforms in your config.json
# This method merges the config.json transforms into the RecordMapper field
# This method merges the config.json transforms into the CollectionSpace::Mapper::RecordMapper field
# mappings for the appropriate fields
def merge_config_transforms
return unless @config[:transforms]
Expand Down
3 changes: 1 addition & 2 deletions lib/collectionspace/mapper/data_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module CollectionSpace
module Mapper
class DataMapper
::DataMapper = CollectionSpace::Mapper::DataMapper
attr_reader :handler, :xphash
attr_accessor :doc, :response
def initialize(response, handler, xphash)
Expand Down Expand Up @@ -39,7 +38,7 @@ def add_short_id
.first
targetnode = @doc.xpath("/document/#{ns}").first
child = Nokogiri::XML::Node.new('shortIdentifier', @doc)
child.content = Identifiers.short_identifier(term, :authority)
child.content = CollectionSpace::Mapper::Tools::Identifiers.short_identifier(term, :authority)
targetnode.add_child(child)
end

Expand Down
25 changes: 12 additions & 13 deletions lib/collectionspace/mapper/data_prepper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
module CollectionSpace
module Mapper
class DataPrepper
::DataPrepper = CollectionSpace::Mapper::DataPrepper
attr_reader :data, :handler, :config
attr_accessor :response, :xphash
def initialize(data, handler)
@handler = handler
@config = @handler.config
@cache = @handler.cache
@response = Mapper::setup_data(data)
@response = CollectionSpace::Mapper::setup_data(data)
if @response.valid?
@data = @response.orig_data.transform_keys(&:downcase)
@response.merged_data = merge_default_values
Expand Down Expand Up @@ -97,21 +96,21 @@ def do_splits(xpath, xphash)
column = mapping[:datacolumn]
data = @response.merged_data.fetch(column, nil)
next if data.nil? || data.empty?
@response.split_data[column] = mapping[:repeats] == 'y' ? SimpleSplitter.new(data, @config).result : [data.strip]
@response.split_data[column] = mapping[:repeats] == 'y' ? CollectionSpace::Mapper::SimpleSplitter.new(data, @config).result : [data.strip]
end
elsif xphash[:is_group] == true && xphash[:is_subgroup] == false
xphash[:mappings].each do |mapping|
column = mapping[:datacolumn]
data = @response.merged_data.fetch(column, nil)
next if data.nil? || data.empty?
@response.split_data[column] = SimpleSplitter.new(data, @config).result
@response.split_data[column] = CollectionSpace::Mapper::SimpleSplitter.new(data, @config).result
end
elsif xphash[:is_group] && xphash[:is_subgroup]
xphash[:mappings].each do |mapping|
column = mapping[:datacolumn]
data = @response.merged_data.fetch(column, nil)
next if data.nil? || data.empty?
@response.split_data[column] = SubgroupSplitter.new(data, @config).result
@response.split_data[column] = CollectionSpace::Mapper::SubgroupSplitter.new(data, @config).result
end
end
end
Expand All @@ -128,9 +127,9 @@ def do_transforms(xpath, xphash)
else
targetdata[column] = data.map do |d|
if d.is_a?(String)
ValueTransformer.new(d, mapping[:transforms], @cache).result
CollectionSpace::Mapper::ValueTransformer.new(d, mapping[:transforms], @cache).result
else
d.map{ |val| ValueTransformer.new(val, mapping[:transforms], @cache).result}
d.map{ |val| CollectionSpace::Mapper::ValueTransformer.new(val, mapping[:transforms], @cache).result}
end
end
end
Expand Down Expand Up @@ -170,7 +169,7 @@ def do_term_handling(xpath, xphash)
data = sourcedata.fetch(column, nil)
next if data.blank?

th = TermHandler.new(mapping, data, @cache)
th = CollectionSpace::Mapper::TermHandler.new(mapping, data, @cache)
@response.transformed_data[column] = th.result
@response.terms << th.terms
end
Expand All @@ -190,19 +189,19 @@ def get_source_type(source_type_string)
def structured_date_transform(data)
data.map do |d|
if d.is_a?(String)
CspaceDate.new(d, @handler.client, @handler.cache, @config).mappable
CollectionSpace::Mapper::Tools::Dates::CspaceDate.new(d, @handler.client, @handler.cache, @config).mappable
else
d.map{ |v| CspaceDate.new(v, @handler.client, @handler.cache, @config).mappable }
d.map{ |v| CollectionSpace::Mapper::Tools::Dates::CspaceDate.new(v, @handler.client, @handler.cache, @config).mappable }
end
end
end

def unstructured_date_transform(data)
data.map do |d|
if d.is_a?(String)
CspaceDate.new(d, @handler.client, @handler.cache, @config).stamp
CollectionSpace::Mapper::Tools::Dates::CspaceDate.new(d, @handler.client, @handler.cache, @config).stamp
else
d.map{ |v| CspaceDate.new(v, @handler.client, @handler.cache, @config).stamp }
d.map{ |v| CollectionSpace::Mapper::Tools::Dates::CspaceDate.new(v, @handler.client, @handler.cache, @config).stamp }
end
end
end
Expand All @@ -212,7 +211,7 @@ def check_data_quality(xpath, xphash)
xphash[:mappings].each do |mapping|
data = xformdata[mapping[:datacolumn]]
next if data.blank?
qc = DataQualityChecker.new(mapping, data)
qc = CollectionSpace::Mapper::DataQualityChecker.new(mapping, data)
@response.warnings << qc.warnings unless qc.warnings.empty?
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/collectionspace/mapper/data_quality_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module CollectionSpace
module Mapper
class DataQualityChecker
::DataQualityChecker = CollectionSpace::Mapper::DataQualityChecker
attr_reader :mapping, :data, :warnings, :terms
def initialize(mapping, data)
@mapping = mapping
Expand Down
3 changes: 0 additions & 3 deletions lib/collectionspace/mapper/data_splitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module CollectionSpace
module Mapper
class DataSplitter
::DataSplitter = CollectionSpace::Mapper::DataSplitter
attr_reader :data, :result
def initialize(data, config)
@data = data.strip
Expand All @@ -14,7 +13,6 @@ def initialize(data, config)
end

class SimpleSplitter < DataSplitter
::SimpleSplitter = CollectionSpace::Mapper::SimpleSplitter
def initialize(data, config)
super
# negative limit parameter turns off suppression of trailing empty fields
Expand All @@ -23,7 +21,6 @@ def initialize(data, config)
end

class SubgroupSplitter < DataSplitter
::SubgroupSplitter = CollectionSpace::Mapper::SubgroupSplitter
def initialize(data, config)
super
# negative limit parameter turns off suppression of trailing empty fields
Expand Down
3 changes: 1 addition & 2 deletions lib/collectionspace/mapper/data_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module CollectionSpace
module Mapper
class DataValidator
::DataValidator = CollectionSpace::Mapper::DataValidator
attr_reader :mapper, :cache, :required_fields
def initialize(record_mapper, cache)
@mapper = record_mapper
Expand All @@ -14,7 +13,7 @@ def initialize(record_mapper, cache)
end

def validate(data)
response = Mapper::setup_data(data)
response = CollectionSpace::Mapper::setup_data(data)
if response.valid?
data = data.transform_keys(&:downcase)
res = check_required_fields(data) unless @required_fields.empty?
Expand Down
21 changes: 0 additions & 21 deletions lib/collectionspace/mapper/map_result.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/collectionspace/mapper/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module CollectionSpace
module Mapper
class Response
::Response = CollectionSpace::Mapper::Response
attr_reader :orig_data
attr_accessor :split_data, :merged_data, :transformed_data, :combined_data, :doc, :errors, :warnings, :identifier, :terms
def initialize(data_hash)
Expand Down
3 changes: 1 addition & 2 deletions lib/collectionspace/mapper/term_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module CollectionSpace
module Mapper
class TermHandler
::TermHandler = CollectionSpace::Mapper::TermHandler
attr_reader :result, :terms
def initialize(mapping, data, cache)
@mapping = mapping
Expand Down Expand Up @@ -52,7 +51,7 @@ def handle_term(val)
refname
else
@terms << term_report.merge({ found: false })
RefName.build(@source_type, @type, @subtype, val, @cache)
CollectionSpace::Mapper::Tools::RefName.build(@source_type, @type, @subtype, val, @cache)
end
end
end
Expand Down
16 changes: 7 additions & 9 deletions lib/collectionspace/mapper/tools/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module CollectionSpace
module Mapper
module Errors
::Errors = CollectionSpace::Mapper::Errors
class ConfigKeyMissingError < StandardError
attr_reader :keys
def initialize(message, keys)
Expand All @@ -17,7 +16,6 @@ class ConfigResponseModeError < StandardError; end

module Tools
class Config
::Config = CollectionSpace::Mapper::Tools::Config
attr_reader :hash
def initialize(hash)
@hash = hash
Expand All @@ -31,18 +29,18 @@ def validate
has_required_keys
rescue => error
error.keys.each do |key|
value = Mapper::DEFAULT_CONFIG[key]
value = CollectionSpace::Mapper::DEFAULT_CONFIG[key]
@hash[key] = value
Mapper::LOGGER.warn("#{error.message}: #{key}. Set to: #{value}")
CollectionSpace::Mapper::LOGGER.warn("#{error.message}: #{key}. Set to: #{value}")
end
end

begin
valid_response_mode
rescue => error
replacement_value = Mapper::DEFAULT_CONFIG[:response_mode]
replacement_value = CollectionSpace::Mapper::DEFAULT_CONFIG[:response_mode]
@hash[:response_mode] = replacement_value
Mapper::LOGGER.warn("#{error.message}. Reset to: #{replacement_value}")
CollectionSpace::Mapper::LOGGER.warn("#{error.message}. Reset to: #{replacement_value}")
end
end

Expand All @@ -52,15 +50,15 @@ def valid_response_mode
valid = %w[normal verbose]
value = @hash[:response_mode]
unless valid.any?(value)
raise Errors::ConfigResponseModeError.new("Invalid response_mode value in config: #{value}")
raise CollectionSpace::Mapper::Errors::ConfigResponseModeError.new("Invalid response_mode value in config: #{value}")
end
end

def has_required_keys
required_keys = Mapper::DEFAULT_CONFIG.keys
required_keys = CollectionSpace::Mapper::DEFAULT_CONFIG.keys
remaining_keys = required_keys - @hash.keys
unless remaining_keys.empty?
raise Errors::ConfigKeyMissingError.new('Config missing key', remaining_keys)
raise CollectionSpace::Mapper::Errors::ConfigKeyMissingError.new('Config missing key', remaining_keys)
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/collectionspace/mapper/tools/dates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ module CollectionSpace
module Mapper
module Tools
module Dates
::Dates = CollectionSpace::Mapper::Tools::Dates
extend self

class CspaceDate
::CspaceDate = CollectionSpace::Mapper::Tools::Dates::CspaceDate
attr_reader :date_string, :client, :cache, :config, :timestamp, :mappable, :stamp

def initialize(date_string, client, cache, config)
Expand Down Expand Up @@ -65,7 +63,6 @@ def try_services_query
def map(doc, parentnode, groupname)
@parser_result.each do |datefield, value|
value = DateTime.parse(value).iso8601(3).sub('+00:00', "Z") if datefield['ScalarValue']

end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/collectionspace/mapper/tools/identifiers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module CollectionSpace
module Mapper
module Tools
module Identifiers
::Identifiers = CollectionSpace::Mapper::Tools::Identifiers
extend self
def short_identifier(name, source_type)
v = name.gsub(/\W/, '')
Expand Down
Loading

0 comments on commit f108174

Please sign in to comment.