Skip to content

Commit

Permalink
Refactor csv_importer and improve test accuracy
Browse files Browse the repository at this point in the history
Previously the "GenericWork" argument was not useful for anything and
the test data did not trigger the initializer argument to be relevant.

The importer was not extensible w/ an arbitrary class because it relied
on exclusively on the `Factory.for` invocation.  Now, as the spec
demonstrates, a user can use an arbitrary or even anonymous class.

Much YARD added.
  • Loading branch information
atz committed Aug 22, 2017
1 parent 05f0bc0 commit 7618c3a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
30 changes: 21 additions & 9 deletions lib/importer/csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ module Importer
# header row. The model for each row can either be specified in a column called
# 'type' or globally by passing the model attribute
class CSVImporter
# @param [String] metadata_file path to CSV file
# @param [String] files_directory path, passed to factory constructor
# @param [#to_s, Class] model if Class, the factory class to be invoked per row.
# Otherwise, the stringable first (Xxx) portion of an "XxxFactory" constant.
def initialize(metadata_file, files_directory, model = nil)
@model = model
@files_directory = files_directory
@metadata_file = metadata_file
@files_directory = files_directory
@model = model
end

# @return [Integer] count of objects created
def import_all
count = 0
parser.each do |attributes|
Expand All @@ -24,16 +29,23 @@ def parser
CSVParser.new(@metadata_file)
end

# Build a factory to create the objects in fedora.
def create_fedora_objects(attributes)
model = attributes.delete(:type) || @model.to_s
# @return [Class] the model class to be used
def factory_class(model)
return model if model.is_a?(Class)
if model.empty?
$stderr.puts 'ERROR: No model was specified'
# rubocop:disable Rails/Exit
exit(1)
# rubocop:enable Rails/Exit
exit(1) # rubocop:disable Rails/Exit
end
Factory.for(model).new(attributes, @files_directory).run
return Factory.for(model.to_s) if model.respond_to?(:to_s)
raise "Unrecognized model type: #{model.class}"
end

# Build a factory to create the objects in fedora.
# @param [Hash<Symbol => String>] attributes
# @option attributes [String] :type overrides model for a single object
# @note remaining attributes are passed to factory constructor
def create_fedora_objects(attributes)
factory_class(attributes.delete(:type) || @model).new(attributes, @files_directory).run
end
end
end
1 change: 1 addition & 0 deletions lib/importer/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Factory
autoload :WithAssociatedCollection
end

# @param [#to_s] First (Xxx) portion of an "XxxFactory" constant
def self.for(model_name)
const_get "#{model_name}Factory"
end
Expand Down
Loading

0 comments on commit 7618c3a

Please sign in to comment.