Module: Bulkrax::EntrySpecHelper
- Defined in:
- lib/bulkrax/entry_spec_helper.rb
Overview
The purpose of this module is to provide some testing facilities for those that include the Bulkrax gem in their application.
This module came about through a desire to expose a quick means of vetting the accuracy of the different parsers.
Constant Summary collapse
- ENTRY_TYPE_TO_METHOD_NAME_MAP =
{ entry: :entry_class, collection: :collection_entry_class, file_set: :file_set_entry_class }.freeze
- DEFAULT_ENTRY_CLASS_TO_SYMBOL_MAP =
{ 'Bulkrax::OaiEntry' => :oai, 'Bulkrax::XmlEntry' => :xml, 'Bulkrax::CsvEntry' => :csv }.freeze
Class Method Summary collapse
-
.entry_class_to_symbol_map ⇒ Object
Present implementations of entry classes tend to inherit from the below listed class names.
- .entry_class_to_symbol_map=(value) ⇒ Object
-
.entry_for(data:, identifier:, parser_class_name:, type: :entry, **options) ⇒ Bulkrax::Entry
The purpose of this method is encapsulate the logic of creating the appropriate Entry object based on the given data, identifier, and parser_class_name.
- .exporter_for(parser_class_name:, parser_fields: {}, **options) ⇒ Bulkrax::Exporter
Class Method Details
.entry_class_to_symbol_map ⇒ Object
Present implementations of entry classes tend to inherit from the below listed class names. We’re not looking to register all descendents of the Bulkrax::Entry class, but instead find the ancestor where there is significant deviation.
136 137 138 |
# File 'lib/bulkrax/entry_spec_helper.rb', line 136 def self.entry_class_to_symbol_map @entry_class_to_symbol_map || DEFAULT_ENTRY_CLASS_TO_SYMBOL_MAP end |
.entry_class_to_symbol_map=(value) ⇒ Object
140 141 142 |
# File 'lib/bulkrax/entry_spec_helper.rb', line 140 def self.entry_class_to_symbol_map=(value) @entry_class_to_symbol_map = value end |
.entry_for(data:, identifier:, parser_class_name:, type: :entry, **options) ⇒ Bulkrax::Entry
In the case of the Bulkrax::CsvParser, the :data keyword is a Hash, where the keys are the column name of the CSV you’re importing. The ‘import_file_path’ is a path to a CSV file. That CSV’s columns does not need to match the :data’s keys, though there may be required headers on that CSV based on the parser implementation.
In the case of an OaiParser, the :data keyword should be a String. And you’ll need to provide a :parser_fields with a “base_url”.
The purpose of this method is encapsulate the logic of creating the appropriate Bulkrax::Entry object based on the given data, identifier, and parser_class_name. Due to the different means of instantiation of Bulkrax::Entry subclasses, there are several optional parameters.
From that entry, you should be able to test how Bulkrax::Entry#build_metadata populates the Bulkrax::Entry#parsed_metadata variable. Other uses may emerge.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bulkrax/entry_spec_helper.rb', line 77 def self.entry_for(data:, identifier:, parser_class_name:, type: :entry, **) importer = importer_for(parser_class_name: parser_class_name, **) entry_type_method_name = ENTRY_TYPE_TO_METHOD_NAME_MAP.fetch(type) entry_class = importer.parser.public_send(entry_type_method_name) # Using an instance of the entry_class to dispatch to different entry_for_dispatch = entry_class.new # Using the {is_a?} test we get the benefit of inspecting an object's inheritance path # (e.g. ancestry). The logic, as implemented, also provides a mechanism for folks in their # applications to add a {class_name_entry_for}; something that I suspect isn't likely # but given the wide variety of underlying needs I could see happening and I want to encourage # patterned thinking to fold that different build method into this structure. key = entry_class_to_symbol_map.keys.detect { |class_name| entry_for_dispatch.is_a?(class_name.constantize) } # Yes, we'll raise an error if we didn't find a corresponding key. And that's okay. symbol = entry_class_to_symbol_map.fetch(key) send("build_#{symbol}_entry_for", importer: importer, identifier: identifier, entry_class: entry_class, data: data, **) end |
.exporter_for(parser_class_name:, parser_fields: {}, **options) ⇒ Bulkrax::Exporter
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/bulkrax/entry_spec_helper.rb', line 110 def self.exporter_for(parser_class_name:, parser_fields: {}, **) Bulkrax::Exporter.new( name: .fetch(:exporter_name, "Test importer for identifier"), user: .fetch(:exporter_user, User.new(email: "[email protected]")), limit: .fetch(:exporter_limit, 1), parser_klass: parser_class_name, field_mapping: .fetch(:exporter_field_mappings) { Bulkrax.field_mappings.fetch(parser_class_name) }, parser_fields: parser_fields ) end |