Module: Backgrounded::ClassMethods

Defined in:
lib/backgrounded/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#backgrounded(*args) ⇒ Object



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

def backgrounded(*args)
  options = args.extract_options!
  methods_with_options = args.inject({}) do |hash, method| hash[method] = {}; hash end
  methods_with_options.merge! options
  methods_with_options.each_pair do |method, options|
    method_basename, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
    if options[:class_method]
      class_eval <<-EOS
        def self.#{method_basename}_backgrounded#{punctuation}(*args)
          Backgrounded.handler.request(self, :#{method}, *args)
        end
      EOS
    else
      define_method "#{method_basename}_backgrounded#{punctuation}" do |*args|
        Backgrounded.handler.request(self, method, *args)
      end
    end
  end
  cattr_accessor :backgrounded_options
  self.backgrounded_options ||= {}
  self.backgrounded_options.merge! methods_with_options
end