Class: MARC::JSONLWriter
Instance Attribute Summary
Attributes inherited from Writer
Class Method Summary collapse
-
.encode(record) ⇒ String
Encode the record as a marc-in-json string.
Instance Method Summary collapse
- #encode(rec) ⇒ Object
-
#initialize(file, &blk) ⇒ JSONLWriter
constructor
A new instance of JSONLWriter.
-
#write(record) ⇒ MARC::JSONLWriter
Write encoded record to the handle.
Methods inherited from Writer
Constructor Details
#initialize(file, &blk) ⇒ JSONLWriter
Returns a new instance of JSONLWriter.
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
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
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
27 28 29 30 |
# File 'lib/marc/jsonl_writer.rb', line 27 def write(record) @fh.puts(encode(record)) self end |