class MARC::JSONLWriter

Public Class Methods

encode(record) click to toggle source

Encode the record as a marc-in-json string @param [MARC::Record] record @return [String] MARC-in-JSON representation of the record

# File lib/marc/jsonl_writer.rb, line 35
def self.encode(record)
  JSON.fast_generate(record.to_hash)
end
new(file, &blk) click to toggle source

@param [String, IO] file A filename, or open File/IO type object, from which to read

# File lib/marc/jsonl_writer.rb, line 9
def initialize(file, &blk)
  if file.instance_of?(String)
    @fh = File.new(file, "w:utf-8")
  elsif file.respond_to?(:write)
    @fh = file
  else
    raise ArgumentError, "must pass in file name or handle"
  end

  if blk
    blk.call(self)
    close
  end
end

Public Instance Methods

encode(rec) click to toggle source

@see MARC::JSONLWriter.encode

# File lib/marc/jsonl_writer.rb, line 40
def encode(rec)
  self.class.encode(rec)
end
write(record) click to toggle source

Write encoded record to the handle @param [MARC::Record] record @return [MARC::JSONLWriter] self

# File lib/marc/jsonl_writer.rb, line 27
def write(record)
  @fh.puts(encode(record))
  self
end