Class: Abroad::Serializers::Txt::LinesSerializer

Inherits:
TxtSerializer show all
Defined in:
lib/abroad/serializers/txt/lines_serializer.rb

Constant Summary collapse

DEFAULT_NEWLINE_SEPARATOR =
"\n"

Constants inherited from Serializer

Serializer::DEFAULT_ENCODING

Instance Attribute Summary collapse

Attributes inherited from Serializer

#locale, #options, #stream

Instance Method Summary collapse

Methods inherited from TxtSerializer

#write_raw

Methods inherited from Serializer

#close, #encoding, from_stream, open, #write_raw

Constructor Details

#initialize(stream, locale, options = {}) ⇒ LinesSerializer

Returns a new instance of LinesSerializer.


10
11
12
13
14
# File 'lib/abroad/serializers/txt/lines_serializer.rb', line 10

def initialize(stream, locale, options = {})
  @newline_separator = options.fetch(:newline_separator, DEFAULT_NEWLINE_SEPARATOR)
  @lines = []
  super
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines


8
9
10
# File 'lib/abroad/serializers/txt/lines_serializer.rb', line 8

def lines
  @lines
end

#newline_separatorObject (readonly)

Returns the value of attribute newline_separator


8
9
10
# File 'lib/abroad/serializers/txt/lines_serializer.rb', line 8

def newline_separator
  @newline_separator
end

Instance Method Details

#flushObject


24
25
26
27
# File 'lib/abroad/serializers/txt/lines_serializer.rb', line 24

def flush
  stream.write(lines.join(newline_separator))
  super
end

#write_key_value(key, value) ⇒ Object


16
17
18
19
20
21
22
# File 'lib/abroad/serializers/txt/lines_serializer.rb', line 16

def write_key_value(key, value)
  unless valid_key?(key)
    raise KeyError, "'#{key}' is not a valid key"
  end

  add_line(key, value)
end