Class: Abroad::Serializers::Serializer

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

Constant Summary collapse

DEFAULT_ENCODING =
Encoding::UTF_8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Serializer.


27
28
29
30
31
# File 'lib/abroad/serializers/serializer.rb', line 27

def initialize(stream, locale, options = {})
  @stream = stream
  @locale = locale
  @options = options
end

Instance Attribute Details

#localeObject (readonly)

Returns the value of attribute locale


7
8
9
# File 'lib/abroad/serializers/serializer.rb', line 7

def locale
  @locale
end

#optionsObject (readonly)

Returns the value of attribute options


7
8
9
# File 'lib/abroad/serializers/serializer.rb', line 7

def options
  @options
end

#streamObject (readonly)

Returns the value of attribute stream


7
8
9
# File 'lib/abroad/serializers/serializer.rb', line 7

def stream
  @stream
end

Class Method Details

.from_stream(stream, locale) ⇒ Object


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

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, mode = 'r', &block) ⇒ Object


22
23
24
# File 'lib/abroad/serializers/serializer.rb', line 22

def open(file, locale, mode = 'r', &block)
  from_stream(File.open(file, mode), locale, &block)
end

Instance Method Details

#closeObject


51
52
53
54
# File 'lib/abroad/serializers/serializer.rb', line 51

def close
  flush
  stream.close
end

#encodingObject


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

def encoding
  options.fetch(:encoding, DEFAULT_ENCODING)
end

#flushObject


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

def flush
  stream.flush
end

#write_key_value(key, value) ⇒ Object

Raises:

  • (NotImplementedError)

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

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

#write_raw(text) ⇒ Object

Raises:

  • (NotImplementedError)

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

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