Class: Rosette::Serializers::Serializer

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

Overview

Base class for all Rosette's serializers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new serializer.

Parameters:

  • stream (#write, #flush)

    The stream to write serialized phrases or translations to.

  • locale (String)

    The locale of the translations to write to stream.

  • encoding (String, Encoding) (defaults to: Encoding::UTF_8)

    The encoding to use when writing the phrases or translations to stream.


59
60
61
62
63
# File 'lib/rosette/serializers/serializer.rb', line 59

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

Instance Attribute Details

#encodingString, Encoding (readonly)

Returns the encoding to use when writing the phrases or translations to stream.

Returns:

  • (String, Encoding)

    the encoding to use when writing the phrases or translations to stream.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rosette/serializers/serializer.rb', line 17

class Serializer
  attr_reader :stream, :locale, :encoding

  class << self    # Creates a new serializer around the given stream.
    #
    # @param [#write, #flush] stream The stream object to write serialized
    #   phrases and translations to.
    # @param [Locale] locale The associated locale.
    # @return [Serializer]

    def from_stream(stream, locale)
      new(stream, locale)
    end

    # Creates a new serializer around the given file. Opens the file and
    # instantiates a new serializer with the handle.
    #
    # @param [String] file The file.
    # @param [Locale] locale The associated locale.
    def open(file, locale)
      new(File.open(file), locale)
    end

    # Returns the default file extension for the file type this serializer
    # generates. For example, if this is the yaml/rails serializer, the
    # default extension would be '.yml'.
    #
    # @raise [NotImplementedError]
    def default_extension
      raise NotImplementedError,
        'expected to be implemented in derived classes'
    end
  end

  # Creates a new serializer.
  #
  # @param [#write, #flush] stream The stream to write serialized phrases
  #   or translations to.
  # @param [String] locale The locale of the translations to write to
  #   +stream+.
  # @param [String, Encoding] encoding The encoding to use when writing the
  #   phrases or translations to +stream+.
  def initialize(stream, locale, encoding = Encoding::UTF_8)
    @stream = stream
    @locale = locale
    @encoding = encoding
  end

  # Serializes and writes a key/value pair to the stream. The key is often
  # a phrase key or meta key, and the value is often a foreign-language
  # translation.
  #
  # @param [String] key The phrase key or meta key.
  # @param [String] value
  # @return [void]
  # @raise [NotImplementedError]
  def write_key_value(key, value)
    raise NotImplementedError,
      'expected to be implemented in derived classes'
  end

  # Writes raw text to +stream+ without serializing it first.
  #
  # @param [String] text The raw text to write.
  # @return [void]
  # @raise [NotImplementedError]
  def write_raw(text)
    raise NotImplementedError,
      'expected to be implemented in derived classes'
  end

  # Flushes any buffered text from +stream+ (i.e. forces buffered text
  # to be written immediately).
  #
  # @return [void]
  def flush
    stream.flush
  end
end

#localeString (readonly)

Returns the locale to expect the phrases or tranlsations to be written in.

Returns:

  • (String)

    the locale to expect the phrases or tranlsations to be written in.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rosette/serializers/serializer.rb', line 17

class Serializer
  attr_reader :stream, :locale, :encoding

  class << self    # Creates a new serializer around the given stream.
    #
    # @param [#write, #flush] stream The stream object to write serialized
    #   phrases and translations to.
    # @param [Locale] locale The associated locale.
    # @return [Serializer]

    def from_stream(stream, locale)
      new(stream, locale)
    end

    # Creates a new serializer around the given file. Opens the file and
    # instantiates a new serializer with the handle.
    #
    # @param [String] file The file.
    # @param [Locale] locale The associated locale.
    def open(file, locale)
      new(File.open(file), locale)
    end

    # Returns the default file extension for the file type this serializer
    # generates. For example, if this is the yaml/rails serializer, the
    # default extension would be '.yml'.
    #
    # @raise [NotImplementedError]
    def default_extension
      raise NotImplementedError,
        'expected to be implemented in derived classes'
    end
  end

  # Creates a new serializer.
  #
  # @param [#write, #flush] stream The stream to write serialized phrases
  #   or translations to.
  # @param [String] locale The locale of the translations to write to
  #   +stream+.
  # @param [String, Encoding] encoding The encoding to use when writing the
  #   phrases or translations to +stream+.
  def initialize(stream, locale, encoding = Encoding::UTF_8)
    @stream = stream
    @locale = locale
    @encoding = encoding
  end

  # Serializes and writes a key/value pair to the stream. The key is often
  # a phrase key or meta key, and the value is often a foreign-language
  # translation.
  #
  # @param [String] key The phrase key or meta key.
  # @param [String] value
  # @return [void]
  # @raise [NotImplementedError]
  def write_key_value(key, value)
    raise NotImplementedError,
      'expected to be implemented in derived classes'
  end

  # Writes raw text to +stream+ without serializing it first.
  #
  # @param [String] text The raw text to write.
  # @return [void]
  # @raise [NotImplementedError]
  def write_raw(text)
    raise NotImplementedError,
      'expected to be implemented in derived classes'
  end

  # Flushes any buffered text from +stream+ (i.e. forces buffered text
  # to be written immediately).
  #
  # @return [void]
  def flush
    stream.flush
  end
end

#stream#write, #flush (readonly)

Returns an IO-style object to write the serialized phrases or translations to.

Returns:

  • (#write, #flush)

    an IO-style object to write the serialized phrases or translations to.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rosette/serializers/serializer.rb', line 17

class Serializer
  attr_reader :stream, :locale, :encoding

  class << self    # Creates a new serializer around the given stream.
    #
    # @param [#write, #flush] stream The stream object to write serialized
    #   phrases and translations to.
    # @param [Locale] locale The associated locale.
    # @return [Serializer]

    def from_stream(stream, locale)
      new(stream, locale)
    end

    # Creates a new serializer around the given file. Opens the file and
    # instantiates a new serializer with the handle.
    #
    # @param [String] file The file.
    # @param [Locale] locale The associated locale.
    def open(file, locale)
      new(File.open(file), locale)
    end

    # Returns the default file extension for the file type this serializer
    # generates. For example, if this is the yaml/rails serializer, the
    # default extension would be '.yml'.
    #
    # @raise [NotImplementedError]
    def default_extension
      raise NotImplementedError,
        'expected to be implemented in derived classes'
    end
  end

  # Creates a new serializer.
  #
  # @param [#write, #flush] stream The stream to write serialized phrases
  #   or translations to.
  # @param [String] locale The locale of the translations to write to
  #   +stream+.
  # @param [String, Encoding] encoding The encoding to use when writing the
  #   phrases or translations to +stream+.
  def initialize(stream, locale, encoding = Encoding::UTF_8)
    @stream = stream
    @locale = locale
    @encoding = encoding
  end

  # Serializes and writes a key/value pair to the stream. The key is often
  # a phrase key or meta key, and the value is often a foreign-language
  # translation.
  #
  # @param [String] key The phrase key or meta key.
  # @param [String] value
  # @return [void]
  # @raise [NotImplementedError]
  def write_key_value(key, value)
    raise NotImplementedError,
      'expected to be implemented in derived classes'
  end

  # Writes raw text to +stream+ without serializing it first.
  #
  # @param [String] text The raw text to write.
  # @return [void]
  # @raise [NotImplementedError]
  def write_raw(text)
    raise NotImplementedError,
      'expected to be implemented in derived classes'
  end

  # Flushes any buffered text from +stream+ (i.e. forces buffered text
  # to be written immediately).
  #
  # @return [void]
  def flush
    stream.flush
  end
end

Class Method Details

.default_extensionObject

Returns the default file extension for the file type this serializer generates. For example, if this is the yaml/rails serializer, the default extension would be '.yml'.

Raises:

  • (NotImplementedError)

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

def default_extension
  raise NotImplementedError,
    'expected to be implemented in derived classes'
end

.from_stream(stream, locale) ⇒ Serializer

Creates a new serializer around the given stream.

Parameters:

  • stream (#write, #flush)

    The stream object to write serialized phrases and translations to.

  • locale (Locale)

    The associated locale.

Returns:


27
28
29
# File 'lib/rosette/serializers/serializer.rb', line 27

def from_stream(stream, locale)
  new(stream, locale)
end

.open(file, locale) ⇒ Object

Creates a new serializer around the given file. Opens the file and instantiates a new serializer with the handle.

Parameters:

  • file (String)

    The file.

  • locale (Locale)

    The associated locale.


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

def open(file, locale)
  new(File.open(file), locale)
end

Instance Method Details

#flushvoid

This method returns an undefined value.

Flushes any buffered text from stream (i.e. forces buffered text to be written immediately).


92
93
94
# File 'lib/rosette/serializers/serializer.rb', line 92

def flush
  stream.flush
end

#write_key_value(key, value) ⇒ void

This method returns an undefined value.

Serializes and writes a key/value pair to the stream. The key is often a phrase key or meta key, and the value is often a foreign-language translation.

Parameters:

  • key (String)

    The phrase key or meta key.

  • value (String)

Raises:

  • (NotImplementedError)

73
74
75
76
# File 'lib/rosette/serializers/serializer.rb', line 73

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

#write_raw(text) ⇒ void

This method returns an undefined value.

Writes raw text to stream without serializing it first.

Parameters:

  • text (String)

    The raw text to write.

Raises:

  • (NotImplementedError)

83
84
85
86
# File 'lib/rosette/serializers/serializer.rb', line 83

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