Module: Yatran::Translatable

Defined in:
lib/yatran/translatable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Overrides @method_missing@ to define and call dynamic translation methods.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yatran/translatable.rb', line 48

def method_missing(method, *args, &block)

   translation = translation_direction(method.to_s)

   if translation.nil?
      super(method, *args, &block)
   else
     self.class.send(:define_method, method.to_s) { translate translation}
     translate translation
   end

end

Class Method Details

.included(base) ⇒ Object



29
30
31
32
33
# File 'lib/yatran/translatable.rb', line 29

def self.included(base)

  base.extend ClassMethods

end

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

Overrides @respond_to?@ to use the dynamic translation methods.

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/yatran/translatable.rb', line 67

def respond_to?(method, include_private = false)

  return true unless translation_direction(method.to_s).nil?
  super(method, include_private)
end

#translate(translation_direction) ⇒ Object



36
37
38
39
40
# File 'lib/yatran/translatable.rb', line 36

def translate(translation_direction)
  response = API.request('translate', {:lang=> translation_direction, :text=>self})['text']
  response =  response[0] unless self.kind_of? Array
  response
end

#translation_direction(method) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/yatran/translatable.rb', line 75

def translation_direction(method)

  method = case method
    when /^to_(.*)_from_(.*)$/
      then "#{$2}-#{$1}"
    when /^from_(.*)_to_(.*)$/
      then "#{$1}-#{$2}"
    when /^(.*)_(.*)$/
      then "#{$1}-#{$2}"
  end

  (LANGUAGES_TRANSLATIONS.include? method ) ? method : nil


end