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 "        def self.\#{method_basename}_backgrounded\#{punctuation}(*args)\n          Backgrounded.handler.request(self, :\#{method}, *args)\n        end\n      EOS\n    else\n      define_method \"\#{method_basename}_backgrounded\#{punctuation}\" do |*args|\n        Backgrounded.handler.request(self, method, *args)\n      end\n    end\n  end\n  cattr_accessor :backgrounded_options\n  self.backgrounded_options ||= {}\n  self.backgrounded_options.merge! methods_with_options\nend\n"