Class: Cacofonix::CodeListExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/cacofonix/utils/code_list_extractor.rb

Overview

A utility class that processes the code list XSD from the ONIX spec and creates a set of TSV files. The generated files are used by this library to make hashes of the code lists available to users.

Constant Summary collapse

FORMATS =
[:tsv, :ruby]

Instance Method Summary collapse

Constructor Details

#initialize(filename, format = :tsv) ⇒ CodeListExtractor

Creates a new extractor. Expects the path to a copy of the code lists file from the spec (called ONIX_BookProduct_CodeLists.xsd on my system).

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
# File 'lib/cacofonix/utils/code_list_extractor.rb', line 16

def initialize(filename, format = :tsv)
  raise ArgumentError, "#{filename} not found" unless File.file?(filename)

  @filename = filename
  raise "Unknown format: #{format}"  unless FORMATS.include?(format)
  @format = format
end

Instance Method Details

#run(dir) ⇒ Object

Generate one file for each list in the specified format in the given directory. Creates the directory if it doesn’t exist. This will overwrite any existing files.



28
29
30
31
32
33
34
# File 'lib/cacofonix/utils/code_list_extractor.rb', line 28

def run(dir)
  FileUtils.mkdir_p(dir)

  each_list do |number, data|
    send("write_to_#{@format}_format", dir, number, data)
  end
end