Class: Abroad::Serializers::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/abroad/serializers/serializer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, locale, encoding = Encoding::UTF_8) ⇒ Serializer

Returns a new instance of Serializer.



25
26
27
28
29
# File 'lib/abroad/serializers/serializer.rb', line 25

def initialize(stream, locale, encoding = Encoding::UTF_8)
  @stream = stream
  @locale = locale
  @encoding = encoding
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding.



5
6
7
# File 'lib/abroad/serializers/serializer.rb', line 5

def encoding
  @encoding
end

#localeObject (readonly)

Returns the value of attribute locale.



5
6
7
# File 'lib/abroad/serializers/serializer.rb', line 5

def locale
  @locale
end

#streamObject (readonly)

Returns the value of attribute stream.



5
6
7
# File 'lib/abroad/serializers/serializer.rb', line 5

def stream
  @stream
end

Class Method Details

.from_stream(stream, locale) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/abroad/serializers/serializer.rb', line 8

def from_stream(stream, locale)
  serializer = new(stream, locale)

  if block_given?
    yield(serializer).tap do
      serializer.close
    end
  else
    serializer
  end
end

.open(file, locale, &block) ⇒ Object



20
21
22
# File 'lib/abroad/serializers/serializer.rb', line 20

def open(file, locale, &block)
  from_stream(File.open(file), locale, &block)
end

Instance Method Details

#closeObject



45
46
47
48
# File 'lib/abroad/serializers/serializer.rb', line 45

def close
  flush
  stream.close
end

#flushObject



41
42
43
# File 'lib/abroad/serializers/serializer.rb', line 41

def flush
  stream.flush
end

#write_key_value(key, value) ⇒ Object

Raises:

  • (NotImplementedError)


31
32
33
34
# File 'lib/abroad/serializers/serializer.rb', line 31

def write_key_value(key, value)
  raise NotImplementedError,
    'expected to be implemented in derived classes'
end

#write_raw(text) ⇒ Object

Raises:

  • (NotImplementedError)


36
37
38
39
# File 'lib/abroad/serializers/serializer.rb', line 36

def write_raw(text)
  raise NotImplementedError,
    'expected to be implemented in derived classes'
end