Class: MARC::JSONLWriter

Inherits:
Writer
  • Object
show all
Defined in:
lib/marc/jsonl_writer.rb

Instance Attribute Summary

Attributes inherited from Writer

#allow_oversized

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Writer

#close, format_byte_count

Constructor Details

#initialize(file, &blk) ⇒ JSONLWriter

Returns a new instance of JSONLWriter.

Parameters:

  • file (String, IO)

    A filename, or open File/IO type object, from which to read



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Class Method Details

.encode(record) ⇒ String

Encode the record as a marc-in-json string

Parameters:

Returns:

  • (String)

    MARC-in-JSON representation of the record



35
36
37
# File 'lib/marc/jsonl_writer.rb', line 35

def self.encode(record)
  JSON.fast_generate(record.to_hash)
end

Instance Method Details

#encode(rec) ⇒ Object

See Also:



40
41
42
# File 'lib/marc/jsonl_writer.rb', line 40

def encode(rec)
  self.class.encode(rec)
end

#write(record) ⇒ MARC::JSONLWriter

Write encoded record to the handle

Parameters:

Returns:



27
28
29
30
# File 'lib/marc/jsonl_writer.rb', line 27

def write(record)
  @fh.puts(encode(record))
  self
end