Class: Recon::ReconManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize the ReconManager.

Parameters:

  • source_type (Symbol)

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

  • out_dir (String)

    the output directory path

  • files (Array<String>)

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



16
17
18
19
20
21
# File 'lib/ds/recon/recon_manager.rb', line 16

def initialize source_type:, out_dir:, files:
  @source_type   = source_type
  @out_dir       = out_dir
  @files         = files
  @errors        = {}
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



8
9
10
# File 'lib/ds/recon/recon_manager.rb', line 8

def files
  @files
end

#out_dirObject (readonly)

Returns the value of attribute out_dir.



6
7
8
# File 'lib/ds/recon/recon_manager.rb', line 6

def out_dir
  @out_dir
end

#source_typeObject (readonly)

Returns the value of attribute source_type.



7
8
9
# File 'lib/ds/recon/recon_manager.rb', line 7

def source_type
  @source_type
end

Instance Method Details

#add_errors(recon_type, messages) ⇒ void

This method returns an undefined value.

Adds errors to the specified recon type.

Parameters:

  • recon_type (ReconType)

    the recon type to add errors to

  • messages (Array<String>)

    the errors to add



69
70
71
72
73
# File 'lib/ds/recon/recon_manager.rb', line 69

def add_errors recon_type, messages
  @errors[recon_type.set_name] ||= []
  @errors[recon_type.set_name] += messages
  nil
end

#errors_for_type(recon_type) ⇒ Array<String>

Returns the list of errors for the specified recon type.

Parameters:

  • recon_type (ReconType)

    the recon type

Returns:

  • (Array<String>)

    the list of errors



87
88
89
# File 'lib/ds/recon/recon_manager.rb', line 87

def errors_for_type recon_type
  @errors[recon_type.set_name]
end

#has_errors?(recon_type) ⇒ Boolean

Returns true if errors exist for the specified recon type.

Parameters:

  • recon_type (ReconType)

    the recon type

Returns:

  • (Boolean)

    true if errors exist



79
80
81
# File 'lib/ds/recon/recon_manager.rb', line 79

def has_errors? recon_type
  errors_for_type(recon_type).present?
end

#recon_builderRecon::ReconBuilder

Initializes and returns a new instance of the Recon::ReconBuilder class with the specified output directory, source type, and files.

Returns:



58
59
60
61
62
# File 'lib/ds/recon/recon_manager.rb', line 58

def recon_builder
  @recon_builder ||= Recon::ReconBuilder.new(
    out_dir: out_dir, source_type: source_type, files: files
  )
end

#write_all_csvsArray<String>

Write all recon CSV files.

Returns:

  • (Array<String>)

    the list of output files



26
27
28
29
30
31
32
# File 'lib/ds/recon/recon_manager.rb', line 26

def write_all_csvs
  outfiles = []
  Recon::RECON_TYPES.each do |recon_type|
    outfiles << write_csv(recon_type)
  end
  outfiles
end

#write_csv(recon_type) ⇒ String

Write a CSV file for a specific recon type.

Parameters:

Returns:

  • (String)

    the path to the output CSV file



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ds/recon/recon_manager.rb', line 38

def write_csv recon_type
  outfile = File.join out_dir, "#{recon_type.set_name}.csv"
  CSV.open(outfile, 'w+', headers: true) do |csv|
    row_num = 0
    csv << recon_type.recon_csv_headers
    recon_builder.each_recon(recon_type.set_name) do |recon|
      errors = Recon.validate_row(recon_type, recon, row_num: row_num += 1)
      add_errors recon_type, errors unless errors.blank?
      csv << recon
    end
  end
  if has_errors?(recon_type)
    raise DSError, "Error writing #{outfile}:\n#{errors_for_type(recon_type).join("\n")}"
  end
  outfile
end