Class: DS::Mapper::BaseMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/ds/mapper/base_mapper.rb

Overview

DS::Mapper::BaseMapper abstract Mapper class. Implementing classes map DS sources records to CSV rows.

Implementing classes must implement:

  • #extract_record

  • #map_record

Direct Known Subclasses

DSCSVMapper, DSMetsMapper, MarcMapper, TeiXmlMapper

Constant Summary collapse

PLACES_COLUMN_MAP =
{
  production_place_as_recorded: :place_as_recorded,
  production_place_ds_qid:      :ds_qid
}
TITLES_COLUMN_MAP =
{
  title_as_recorded:         :title_as_recorded,
  title_as_recorded_agr:     :title_as_recorded_agr,
  uniform_title_as_recorded: :uniform_title_as_recorded,
  uniform_title_agr:         :uniform_title_as_recorded_agr,
  standard_title_ds_qid:     :ds_qid
}
GENRES_COLUMN_MAP =
{
  genre_as_recorded: :genre_as_recorded,
  genre_ds_qid:      :ds_qid
}
SUBJECTS_COLUMN_MAP =
{
  subject_as_recorded: :subject_as_recorded,
  subject_ds_qid:      :ds_qid,
}
AUTHORS_COLUMN_MAP =
{
  author_as_recorded:     :name_as_recorded,
  author_as_recorded_agr: :name_agr,
  author_ds_qid:          :ds_qid

}
ARTISTS_COLUMN_MAP =
{
  artist_as_recorded:     :name_as_recorded,
  artist_as_recorded_agr: :name_agr,
  artist_ds_qid:          :ds_qid
}
SCRIBES_COLUMN_MAP =
{
  scribe_as_recorded:     :name_as_recorded,
  scribe_as_recorded_agr: :name_agr,
  scribe_ds_qid:          :ds_qid
}
ASSOCIATED_AGENT_COLUMN_MAP =
{
  associated_agent_as_recorded:     :name_as_recorded,
  associated_agent_as_recorded_agr: :name_agr,
  associated_agent_ds_qid:          :ds_qid
}
LANGUAGE_COLUMN_MAP =
{
  language_as_recorded: :language_as_recorded,
  language_ds_qid:      :ds_qid
}
FORMER_OWNER_COLUMN_MAP =
{
  former_owner_as_recorded:     :name_as_recorded,
  former_owner_as_recorded_agr: :name_agr,
  former_owner_ds_qid:          :ds_qid
}
MATERIAL_COLUMN_MAP =
{
  material_as_recorded: :material_as_recorded,
  material_ds_qid:      :ds_qid
}
RECON_TYPE_COLUMN_MAP =

Maps recon type to column map

{
  Recon::Type::Places           => PLACES_COLUMN_MAP,
  Recon::Type::Titles           => TITLES_COLUMN_MAP,
  Recon::Type::Genres           => GENRES_COLUMN_MAP,
  Recon::Type::AllSubjects      => SUBJECTS_COLUMN_MAP,
  Recon::Type::Authors          => AUTHORS_COLUMN_MAP,
  Recon::Type::Artists          => ARTISTS_COLUMN_MAP,
  Recon::Type::Scribes          => SCRIBES_COLUMN_MAP,
  Recon::Type::AssociatedAgents => ASSOCIATED_AGENT_COLUMN_MAP,
  Recon::Type::Languages        => LANGUAGE_COLUMN_MAP,
  Recon::Type::FormerOwners     => FORMER_OWNER_COLUMN_MAP,
  Recon::Type::Materials        => MATERIAL_COLUMN_MAP,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_dir:, timestamp:, source:) ⇒ void

Initializes a new instance of the class.

Parameters:

  • source_dir (String)

    the directory where the source files are located

  • timestamp (Time)

    the timestamp of the source files

  • source (DS::Source::BaseSource)

    the source object



104
105
106
107
108
109
# File 'lib/ds/mapper/base_mapper.rb', line 104

def initialize source_dir:, timestamp:, source:
  @recon_builder = Recon::ReconBuilder.new source_type: source.source_type, files: [], out_dir: []
  @source        = source
  @source_dir    = source_dir
  @timestamp     = timestamp
end

Instance Attribute Details

#recon_builderObject (readonly)

Returns the value of attribute recon_builder.



17
18
19
# File 'lib/ds/mapper/base_mapper.rb', line 17

def recon_builder
  @recon_builder
end

#sourceObject (readonly)

Returns the value of attribute source.



16
17
18
# File 'lib/ds/mapper/base_mapper.rb', line 16

def source
  @source
end

#source_dirObject (readonly)

Returns the value of attribute source_dir.



15
16
17
# File 'lib/ds/mapper/base_mapper.rb', line 15

def source_dir
  @source_dir
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



14
15
16
# File 'lib/ds/mapper/base_mapper.rb', line 14

def timestamp
  @timestamp
end

Instance Method Details

#build_term_maps(extractor, record) ⇒ Hash<Symbol, String>

Creates an import CSV hash for the given record for all recon term types, using the given extractor. The extractor is one of

DS::Extractor::MarcXml
DS::Extractor::TeiXml
DS::Extractor::DsCsvExtractor
DS::Extractor::DsMetsXml

The following recon term types are mapped for all records/extractors:

Recon::Type::Places
Recon::Type::Titles
Recon::Type::Genres
Recon::Type::Subjects
Recon::Type::Authors
Recon::Type::Artists
Recon::Type::Scribes
Recon::Type::AssociatedAgents
Recon::Type::Languages
Recon::Type::FormerOwners
Recon::Type::Materials

Column mappings are defined in DS::Mapper::RECON_TYPE_COLUMN_MAP

Parameters:

Returns:

  • (Hash<Symbol, String>)

    a hash of terms mapped to import CSV columns



184
185
186
187
188
189
# File 'lib/ds/mapper/base_mapper.rb', line 184

def build_term_maps extractor, record
  RECON_TYPE_COLUMN_MAP.inject({}) { |hash, (recon_type, column_map)|
    terms = recon_type.method_name.flat_map { |method| extractor.send(method, record) }
    hash.update map_terms terms, recon_type, column_map
  }
end

#build_term_string(recons, recon_key) ⇒ String

Builds a term string by concatenating the values of the given reconstructions corresponding to the specified recon key, separated by ‘|’.

Examples:

recons = [
    { as_recorded: 'Brown, Jamie', authorized_label: 'Jamie Brown' },
    { as_recorded: 'Hendrix, Morgan', authorized_label: 'Morgan Hendrix' }
]
build_term_string(recons, :as_recorded) # => 'Brown, Jamie|Hendrix, Morgan'

Parameters:

  • recons (Array<Hash>)

    The array of recons hashes

  • recon_key (String, Symbol)

    The key used to access the values in each recon hash

Returns:

  • (String)

    The concatenated term string.



204
205
206
# File 'lib/ds/mapper/base_mapper.rb', line 204

def build_term_string recons, recon_key
  recons.map { |recon| recon[recon_key.to_sym] }.join('|')
end

#build_term_strings(recons, column_map) ⇒ Hash

Builds term strings based on the given recons and column mapping.

Examples:

recons = [
    { as_recorded: 'Brown, Jamie', authorized_label: 'Jamie Brown' },
    { as_recorded: 'Hendrix, Morgan', authorized_label: 'Morgan Hendrix' }
]
column_map = { author_as_recorded: :as_recorded, author_label: :authorized_label }
build_term_strings(recons, column_map)
# => { 'author_as_recorded' => 'Brown, Jamie| Hendrix, Morgan', :author_label => 'Jamie Brown|Morgan Hendrix', 'author_label' => 'Jamie Brown|Morgan Hendrix' }

Parameters:

  • recons (Array<Hash>)

    the recons to build term strings from

  • column_map (Hash)

    a mapping of import CSV columns to recon keys

Returns:

  • (Hash)

    a hash with import CSV columns as keys and corresponding term strings as values



148
149
150
151
152
153
# File 'lib/ds/mapper/base_mapper.rb', line 148

def build_term_strings recons, column_map
  column_map.inject({}) do |hash, (import_csv_col, recon_key)|
    hash[import_csv_col] = build_term_string recons, recon_key
    hash
  end
end

#extract_record(entry) ⇒ Object

Extracts a record from the source for the given manifest entry.

Parameters:

Returns:

  • (Object)

    the extracted record; e.g., a Nokogiri::XML::Node or CSV::Row

Raises:

  • (NotImplementedError)

    if the method is not implemented in a subclass



120
121
122
# File 'lib/ds/mapper/base_mapper.rb', line 120

def extract_record entry
  raise NotImplementedError
end

#map_record(entry) ⇒ Hash<Symbol, String>

Maps a source record for the given manifest entry.

Parameters:

Returns:

  • (Hash<Symbol, String>)

    the mapped record

Raises:

  • (NotImplementedError)

    if the method is not implemented in a subclass



129
130
131
# File 'lib/ds/mapper/base_mapper.rb', line 129

def map_record entry
  raise NotImplementedError
end

#map_terms(terms, recon_type, column_map) ⇒ Hash<Symbol,String>

Maps the given terms using the given recon type and column mapping.

Parameters:

Returns:

  • (Hash<Symbol,String>)

    an hash of mapped terms; e.g., { :author_as_recorded => ‘Brown, Jamie|Hendrix, Morgan’, :author_label => ‘Jamie Brown|Morgan Hendrix’, … } for an array of mapped terms



214
215
216
217
218
# File 'lib/ds/mapper/base_mapper.rb', line 214

def map_terms terms, recon_type, column_map
  recons = recon_builder.build_all_recons terms, recon_type
  term_strings = build_term_strings recons, column_map
  term_strings
end

#to_sObject

:nodoc:



111
112
113
# File 'lib/ds/mapper/base_mapper.rb', line 111

def to_s # :nodoc:
  "#{self.class.name}: source_dir: #{source_dir}, timestamp: #{timestamp}, source: #{source}"
end