Sha256: f7c579d052802a18abd75c885ede95b7d7e55908e289599f9b272703f7623669

Contents?: true

Size: 1.28 KB

Versions: 50

Compression:

Stored size: 1.28 KB

Contents

module MongoModel
  module CallbackHelpers
    extend ActiveSupport::Concern
    
    included do
      MongoModel::Callbacks::CALLBACKS.each do |callback_method|
        next if callback_method.to_s =~ /^around_/
        define_callback_method(callback_method)
        send(callback_method, callback_string(callback_method))
        send(callback_method, callback_proc(callback_method))
        send(callback_method, callback_object(callback_method))
        send(callback_method) { |model| model.history << [callback_method, :block] }
      end
    end
    
    module ClassMethods
      def callback_string(callback_method)
        "history << [#{callback_method.to_sym.inspect}, :string]"
      end

      def callback_proc(callback_method)
        Proc.new { |model| model.history << [callback_method, :proc] }
      end

      def define_callback_method(callback_method)
        define_method("#{callback_method}_method") do |model|
          model.history << [callback_method, :method]
        end
      end

      def callback_object(callback_method)
        klass = Class.new
        klass.send(:define_method, callback_method) do |model|
          model.history << [callback_method, :object]
        end
        klass.new
      end
    end
    
    def history
      @history ||= []
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
mongomodel-0.5.5 spec/support/callbacks.rb
mongomodel-0.5.4 spec/support/callbacks.rb
mongomodel-0.5.3 spec/support/callbacks.rb
mongomodel-0.5.2 spec/support/callbacks.rb
mongomodel-0.5.1 spec/support/callbacks.rb
mongomodel-0.5.0 spec/support/callbacks.rb
mongomodel-0.4.9 spec/support/callbacks.rb
mongomodel-0.4.8 spec/support/callbacks.rb
mongomodel-0.4.7 spec/support/callbacks.rb
mongomodel-0.4.6 spec/support/callbacks.rb
mongomodel-0.4.5 spec/support/callbacks.rb
mongomodel-0.4.4 spec/support/callbacks.rb
mongomodel-0.4.3 spec/support/callbacks.rb
mongomodel-0.4.2 spec/support/callbacks.rb
mongomodel-0.4.1 spec/support/callbacks.rb
mongomodel-0.4.0 spec/support/callbacks.rb
mongomodel-0.3.6 spec/support/callbacks.rb
mongomodel-0.3.5 spec/support/callbacks.rb
mongomodel-0.3.4 spec/support/callbacks.rb
mongomodel-0.3.3 spec/support/callbacks.rb