Method: MARC::Reader#initialize

Defined in:
lib/marc/reader.rb

#initialize(file, options = {}) ⇒ Reader

The constructor which you may pass either a path

reader = MARC::Reader.new('marc.dat')

or, if it’s more convenient a File object:

fh = File.new('marc.dat')
reader = MARC::Reader.new(fh)

or really any object that responds to read(n)

# marc is a string with a bunch of records in it
reader = MARC::Reader.new(StringIO.new(marc))

If your data have non-standard control fields in them (e.g., Aleph’s ‘FMT’) you need to add them specifically to the MARC::ControlField.control_tags Set object

MARC::ControlField.control_tags << 'FMT'

Also, if your data encoded with non ascii/utf-8 encoding (for ex. when reading RUSMARC data) and you use ruby 1.9 you can specify source data encoding with an option.

reader = MARC::Reader.new('marc.dat', :external_encoding => 'cp866')

or, you can pass IO, opened in the corresponding encoding

reader = MARC::Reader.new(File.new('marc.dat', 'r:cp866'))


192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/marc/reader.rb', line 192

def initialize(file, options = {})
  @encoding_options = {}
  # all can be nil
  [:internal_encoding, :external_encoding, :invalid, :replace, :validate_encoding].each do |key|
    @encoding_options[key] = options[key] if options.has_key?(key)
  end

  if file.is_a?(String)
    @handle = File.new(file)
  elsif file.respond_to?(:read, 5)
    @handle = file
  else
    raise ArgumentError, "must pass in path or file"
  end

  if (!@encoding_options[:external_encoding]) && @handle.respond_to?(:external_encoding)
    # use file encoding only if we didn't already have an explicit one,
    # explicit one takes precedence.
    #
    # Note, please don't use ruby's own internal_encoding transcode
    # with binary marc data, the transcode can mess up the byte count
    # and make it unreadable.
    @encoding_options[:external_encoding] ||= @handle.external_encoding
  end

  # Only pull in the MARC8 translation if we need it, since it's really big
  if @encoding_options[:external_encoding] == "MARC-8"
    require "marc/marc8/to_unicode" unless defined? MARC::Marc8::ToUnicode
  end
end