Class: Rosetta::Translation

Inherits:
Object
  • Object
show all
Extended by:
Support::Registerable
Defined in:
lib/rosetta/translation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Registerable

registerable_as

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

#deserializerObject (readonly)

Returns the value of attribute deserializer.



17
18
19
# File 'lib/rosetta/translation.rb', line 17

def deserializer
  @deserializer
end

#serializerObject (readonly)

Returns the value of attribute serializer.



17
18
19
# File 'lib/rosetta/translation.rb', line 17

def serializer
  @serializer
end

#translatorObject (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_keyObject



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_procObject



41
42
43
# File 'lib/rosetta/translation.rb', line 41

def to_proc
  proc { |*args, &block| self.call(*args, &block) }
end