Class: Rosette::Serializers::Serializer
- Inherits:
-
Object
- Object
- Rosette::Serializers::Serializer
- Defined in:
- lib/rosette/serializers/serializer.rb
Overview
Base class for all Rosette’s serializers.
Instance Attribute Summary collapse
-
#encoding ⇒ String, Encoding
readonly
The encoding to use when writing the phrases or translations to
stream. -
#locale ⇒ String
readonly
The locale to expect the phrases or tranlsations to be written in.
-
#stream ⇒ #write, #flush
readonly
An IO-style object to write the serialized phrases or translations to.
Class Method Summary collapse
-
.default_extension ⇒ Object
Returns the default file extension for the file type this serializer generates.
-
.from_stream(stream, locale) ⇒ Serializer
Creates a new serializer around the given stream.
-
.open(file, locale) ⇒ Object
Creates a new serializer around the given file.
Instance Method Summary collapse
-
#flush ⇒ void
Flushes any buffered text from
stream(i.e. forces buffered text to be written immediately). -
#initialize(stream, locale, encoding = Encoding::UTF_8) ⇒ Serializer
constructor
Creates a new serializer.
-
#write_key_value(key, value) ⇒ void
Serializes and writes a key/value pair to the stream.
-
#write_raw(text) ⇒ void
Writes raw text to
streamwithout serializing it first.
Constructor Details
#initialize(stream, locale, encoding = Encoding::UTF_8) ⇒ Serializer
Creates a new serializer.
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
#encoding ⇒ String, Encoding (readonly)
Returns 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 |
#locale ⇒ String (readonly)
Returns 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.
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_extension ⇒ Object
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’.
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.
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.
36 37 38 |
# File 'lib/rosette/serializers/serializer.rb', line 36 def open(file, locale) new(File.open(file), locale) end |
Instance Method Details
#flush ⇒ void
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.
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.
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 |