Module: Qe::Locale

Defined in:
lib/qe/locale.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/qe/locale.rb', line 3

def self.included(base)
  unless base.included_modules.include?(Qe::Worker)
    raise OutOfOrderError, "must be included after Qe::Worker"
  end

  class << base
    # Intercept <tt>Worker.enqueue</tt> method, adding
    # the current locale to the options.
    enqueue = instance_method(:enqueue)
    define_method :enqueue do |options = {}|
      options[:locale] ||= I18n.locale
      enqueue.bind(self).call(options)
    end
  end

  base.class_eval do
    # Intercept <tt>Worker#before</tt> method and
    # set the current locale.
    before = instance_method(:before)
    define_method :before do
      I18n.locale = options.delete(:locale)
      before.bind(self).call
    end
  end
end