Skip to content

Commit

Permalink
Split initialization and subsequent logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kspurgin committed Aug 30, 2024
1 parent dac92ef commit 6829e9c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/data_prepper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def do_term_handling(xpath)
data: data,
handler: handler,
response: response
)
).call
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/date_details/data_prepper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def handle_term_fields
data: data,
handler: handler,
response: response
)
).call
end
end

Expand Down
56 changes: 42 additions & 14 deletions lib/collectionspace/mapper/term_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ module Mapper
class TermHandler
include TermSearchable

class << self
# @param mapping [CollectionSpace::Mapper::ColumnMapping]
# @param data [Array<String>]
# @param handler [CollectionSpace::Mapper::DataHandler]
# @param response [CollectionSpace::Mapper::Response]
def call(mapping:, data:, handler:, response:)
new(
mapping: mapping,
data: data,
handler: handler,
response: response
).call
end
end

# @param mapping [CollectionSpace::Mapper::ColumnMapping]
# @param data [Array<String>]
# @param handler [CollectionSpace::Mapper::DataHandler]
Expand All @@ -14,26 +29,39 @@ def initialize(mapping:, data:, handler:, response:)
@data = data
@handler = handler
@response = response
end

@column = mapping.datacolumn
@field = mapping.fieldname
@source_type = mapping.source_type.to_sym
case @source_type
when :authority
authconfig = mapping.transforms[:authority]
@type = authconfig[0]
@subtype = authconfig[1]
when :vocabulary
@type = "vocabularies"
@subtype = mapping.transforms[:vocabulary]
end
def call
response.transformed_data[column] = handle_terms
end

private

attr_reader :mapping, :data, :handler, :response, :column, :source_type,
:type, :subtype
attr_reader :mapping, :data, :handler, :response

def column = @column ||= mapping.datacolumn

# def field = @field ||= mapping.fieldname

def source_type = @source_type ||= mapping.source_type.to_sym

def type
return @type if instance_variable_defined?(:@type)

case @source_type
when :authority then mapping.transforms[:authority][0]
when :vocabulary then "vocabularies"
end
end

def subtype
return @subtype if instance_variable_defined?(:@subtype)

case @source_type
when :authority then mapping.transforms[:authority][1]
when :vocabulary then mapping.transforms[:vocabulary]
end
end

def handle_terms
if data.first.is_a?(String)
Expand Down
4 changes: 2 additions & 2 deletions spec/collectionspace/mapper/term_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
data: data,
handler: handler,
response: response
)
).call
end

let(:handler) do
Expand Down Expand Up @@ -119,7 +119,7 @@
data: data,
handler: handler,
response: new_resp
)
).call

chk = new_resp.terms.select { |h| !h.found? }
expect(chk.length).to eq(1)
Expand Down

0 comments on commit 6829e9c

Please sign in to comment.