Class: Recon::ReconBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ds/recon/recon_builder.rb

Overview

A class to build recon CSV rows from DS data sources.#

Constant Summary collapse

SOURCE_TYPE_ENUMERATORS =

A hash mapping DS data source types to their corresponding enumerator classes

Keys:

  • DS::Constants::DS_CSV
  • DS::Constants::MARC_XML
  • DS::Constants::TEI_XML
  • DS::Constants::DS_METS

Values:

  • Recon::DsCsvEnumerator
  • Recon::MarcXmlEnumerator
  • Recon::TeiXmlEnumerator
  • Recon::DsMetsXmlEnumerator
{
  DS::Constants::DS_CSV => Recon::DsCsvEnumerator,
  DS::Constants::MARC_XML => Recon::MarcXmlEnumerator,
  DS::Constants::TEI_XML => Recon::TeiXmlEnumerator,
  DS::Constants::DS_METS => Recon::DsMetsXmlEnumerator,
}
SOURCE_TYPE_EXTRACTORS =

A hash mapping DS data source types to their corresponding extractor classes

Keys:

  • DS::Constants::DS_CSV
  • DS::Constants::MARC_XML
  • DS::Constants::TEI_XML
  • DS::Constants::DS_METS

Values:

  • DS::Extractor::DsCsv
  • DS::Extractor::MarcXml
  • DS::Extractor::TeiXml
  • DS::Extractor::DsMetsXml
{
  DS::Constants::MARC_XML => DS::Extractor::MarcXmlExtractor,
  DS::Constants::DS_CSV   => DS::Extractor::DsCsvExtractor,
  DS::Constants::DS_METS  => DS::Extractor::DsMetsXmlExtractor,
  DS::Constants::TEI_XML  => DS::Extractor::TeiXml
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_type:, files:, out_dir:) ⇒ ReconBuilder

Returns a new instance of ReconBuilder.

Parameters:

  • source_type (Symbol) —

    a valid DS data source type; e.g., DS::Constants::MARC_XML

  • files (Array) —

    an array of source file paths;e.g., marc1.xml, marc2.xml, etc.

  • out_dir (String) —

    a path to an output directory



59
60
61
62
63
# File 'lib/ds/recon/recon_builder.rb', line 59

def initialize source_type:, files:, out_dir:
  @source_type = source_type
  @files       = files
  @out_dir     = out_dir
end

Instance Attribute Details

#files ⇒ Object (readonly)

Returns the value of attribute files.



13
14
15
# File 'lib/ds/recon/recon_builder.rb', line 13

def files
  @files
end

#out_dir ⇒ Object (readonly)

Returns the value of attribute out_dir.



14
15
16
# File 'lib/ds/recon/recon_builder.rb', line 14

def out_dir
  @out_dir
end

#source_type ⇒ Object (readonly)

Returns the value of attribute source_type.



12
13
14
# File 'lib/ds/recon/recon_builder.rb', line 12

def source_type
  @source_type
end

Instance Method Details

#build_all_recons(terms, recon_type) ⇒ Array<Hash<Symbol,String>>

Builds an array of recon CSV rows for each term in the given terms array using the given recon type configuration.

Parameters:

Returns:

  • (Array<Hash<Symbol,String>>) —

    an array of recon CSV rows



139
140
141
# File 'lib/ds/recon/recon_builder.rb', line 139

def build_all_recons terms, recon_type
  terms.map { |term| build_recon item: term, recon_type: recon_type }
end

#build_recon(item:, recon_type:) ⇒ Hash<Symbol,String>

Build a single recon CSV row; e.g.,

{ :language_as_recorded => "Arabic",
 :language_code => "",
 :authorized_label => "Arabic",
 :structured_value => "Q13955" }

Parameters:

Returns:

  • (Hash<Symbol,String>) —

    a recon CSV row



153
154
155
156
157
158
159
160
161
# File 'lib/ds/recon/recon_builder.rb', line 153

def build_recon item:, recon_type:
  recon_hash = item.to_h
  key_values = recon_type.get_key_values recon_hash
  recon_type.lookup_columns.each do |col|
    val = Recon.lookup_single(recon_type.set_name, key_values: key_values, column: col)
    recon_hash[col] = fix_delimiters val, recon_type.delimiter_map
  end
  recon_hash
end

#each_recon(set_name) {|Hash<Symbol, String>| ... } ⇒ Object

For each extracted term in of the given set type (like :places) yield the corresponding recon CSV row.

Example:

# for a ReconBuilder for set of TEI files
CSV headers: true do |csv|
  csv << Recon::Type::Places.csv_headers
  recon_builder.each_recon(:places) do |recon|
    csv << recon
  end
end

Parameters:

  • set_name (Symbol) —

    a recon set name, like :places

Yields:

  • (Hash<Symbol, String>) —

    a block that yields recon rows



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ds/recon/recon_builder.rb', line 94

def each_recon set_name, &block
  items = Set.new
  recon_type = Recon.find_recon_type set_name

  enumerator.each do |record|
    [recon_type.method_name].flatten.each do |name|
      next unless extractor.respond_to? name.to_sym
      extractor.send(name.to_sym, record).each do |item|
        next if items.include? item
        items << item
        yield build_recon recon_type: recon_type, item: item
      end
    end
  end
end

#enumerator ⇒ Recon::SourceEnumerator

Returns an source enumerator type; e.g., Recon::MarcXmlEnumerator.

Returns:



66
67
68
69
70
71
# File 'lib/ds/recon/recon_builder.rb', line 66

def enumerator
  return @enumerator if @enumerator.present?
  klass = SOURCE_TYPE_ENUMERATORS[source_type]
  raise "Unknown source type: #{source_type}" unless klass
  @enumerator = klass.new files
end

#extractor ⇒ DS::Extractor::MarcXml, ...

Returns an extractor type; e.g., DS::Extractor::MarcXml.

Returns:



74
75
76
# File 'lib/ds/recon/recon_builder.rb', line 74

def extractor
  @extractor ||= SOURCE_TYPE_EXTRACTORS[source_type]
end

#find_recon_type(name) ⇒ Recon::Type::ReconType?

Find a recon type configuration by name

Parameters:

  • name (String) —

    the name of the recon type to find

Returns:



114
115
116
# File 'lib/ds/recon/recon_builder.rb', line 114

def find_recon_type name
  Recon::RECON_TYPES.find { |config| config.set_name == name.to_s }
end

#fix_delimiters(value, delimiter_map = {}) ⇒ String

A function that replaces delimiters in a value based on a given delimiter map.

Parameters:

  • value (String) —

    the value to be processed

  • delimiter_map (Hash) (defaults to: {}) —

    a hash containing the old and new delimiters, e.g., for { "|" => ";" } all +|+s will be replaced with +;+s

Returns:

  • (String) —

    the processed value with delimiters replaced



126
127
128
129
130
131
# File 'lib/ds/recon/recon_builder.rb', line 126

def fix_delimiters value, delimiter_map = {}
  return value if delimiter_map.blank?
  val = ''
  delimiter_map.each  { |old, new| val = value.to_s.gsub old, new }
  val
end