Class: Rosetta::Translation
- Inherits:
-
Object
- Object
- Rosetta::Translation
- Extended by:
- Support::Registerable
- Defined in:
- lib/rosetta/translation.rb
Instance Attribute Summary collapse
-
#deserializer ⇒ Object
readonly
Returns the value of attribute deserializer.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
-
#translator ⇒ Object
(also: #translator?)
readonly
Returns the value of attribute translator.
Class Method Summary collapse
Instance Method Summary collapse
- #call(input) ⇒ Object
-
#initialize(deserializer, serializer) ⇒ Translation
constructor
A new instance of Translation.
- #to_proc ⇒ Object
Methods included from Support::Registerable
Constructor Details
#initialize(deserializer, serializer) ⇒ Translation
Returns a new instance of Translation.
20 21 22 23 24 25 26 |
# File 'lib/rosetta/translation.rb', line 20 def initialize(deserializer, serializer) @translator = Translation[deserializer => serializer] unless @translator @serializer = find_serializer(serializer) @deserializer = find_deserializer(deserializer) end end |
Instance Attribute Details
#deserializer ⇒ Object (readonly)
Returns the value of attribute deserializer.
17 18 19 |
# File 'lib/rosetta/translation.rb', line 17 def deserializer @deserializer end |
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
17 18 19 |
# File 'lib/rosetta/translation.rb', line 17 def serializer @serializer end |
#translator ⇒ Object (readonly) Also known as: translator?
Returns the value of attribute translator.
17 18 19 |
# File 'lib/rosetta/translation.rb', line 17 def translator @translator end |
Class Method Details
.register(source, destination, object = nil, &block) ⇒ Object
12 13 14 |
# File 'lib/rosetta/translation.rb', line 12 def register(source, destination, object=nil, &block) register_key({ source => destination }, object, &block) end |
.register_key ⇒ Object
10 |
# File 'lib/rosetta/translation.rb', line 10 alias_method :register_key, :register |
Instance Method Details
#call(input) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rosetta/translation.rb', line 28 def call(input) if translator? translator.call(input) else elements = deserializer.call(input) serializer.call(elements) end rescue ConversionError => e raise e # No double-wrapping conversion errors rescue StandardError raise TranslationError end |
#to_proc ⇒ Object
41 42 43 |
# File 'lib/rosetta/translation.rb', line 41 def to_proc proc { |*args, &block| self.call(*args, &block) } end |