Sha256: 78fab366dc14aa057797f81a39d1978796219d3a437d66ff0b5d4967d62edf5c

Contents?: true

Size: 1.34 KB

Versions: 12

Compression:

Stored size: 1.34 KB

Contents

module Softwear
  module Lib
    module Enqueue
      extend ActiveSupport::Concern

      module ClassMethods
        def enqueue(*method_names)
          if method_names.last.is_a?(Hash)
            options = method_names.pop
          else
            options = {}
          end
          raise "enqueue what?" if method_names.empty?

          method_names.each do |method_name|
            class_eval <<-RUBY, __FILE__, __LINE__ + 1
              def self.#{method_name}(id, *args)
                find(id).#{method_name}(*args)
              end
            RUBY

            if Rails.env.production?
              class_eval <<-RUBY, __FILE__, __LINE__ + 1
                def enqueue_#{method_name}(*args)
                  self.class.delay(#{options.inspect}).#{method_name}(id, *args)
                end
              RUBY
            else
              class_eval <<-RUBY, __FILE__, __LINE__ + 1
                def enqueue_#{method_name}(*args)
                  self.#{method_name}(*args)
                end
              RUBY
            end
          end

          r = method_names.map { |m| "enqueue_#{m}".to_sym }
          r.size == 1 ? r.first : r
        end
      end

      included do
        extend ClassMethods

        def enqueue(method_name, *args)
          send("enqueue_#{method_name}", *args)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
softwear-lib-1.7.19 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.18 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.17 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.16 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.15 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.13 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.11 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.10 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.9 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.8 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.7 lib/softwear/lib/enqueue.rb
softwear-lib-1.7.6 lib/softwear/lib/enqueue.rb