Class: JSONSEQ::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonseq/writer.rb

Constant Summary collapse

DEFAULT_ENCODER =
-> (object) { JSON.dump(object) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io:, encoder: DEFAULT_ENCODER) ⇒ Writer

Returns a new instance of Writer.



8
9
10
11
# File 'lib/jsonseq/writer.rb', line 8

def initialize(io:, encoder: DEFAULT_ENCODER)
  @io = io
  @encoder = encoder
end

Instance Attribute Details

#encoderObject (readonly)

Returns the value of attribute encoder.



6
7
8
# File 'lib/jsonseq/writer.rb', line 6

def encoder
  @encoder
end

#ioObject (readonly)

Returns the value of attribute io.



5
6
7
# File 'lib/jsonseq/writer.rb', line 5

def io
  @io
end

Instance Method Details

#<<(object) ⇒ Object



13
14
15
16
17
18
# File 'lib/jsonseq/writer.rb', line 13

def <<(object)
  io.write RS
  io.write encoder[object]
  io.write LF
  io.flush
end

#write(object) ⇒ Object



20
21
22
# File 'lib/jsonseq/writer.rb', line 20

def write(object)
  self << object
end