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.3.2 spec/support/callbacks.rb
mongomodel-0.3.1 spec/support/callbacks.rb
mongomodel-0.3.0 spec/support/callbacks.rb
mongomodel-0.2.20 spec/support/callbacks.rb
mongomodel-0.2.19 spec/support/callbacks.rb
mongomodel-0.2.18 spec/support/callbacks.rb
mongomodel-0.2.17 spec/support/callbacks.rb
mongomodel-0.2.16 spec/support/callbacks.rb
mongomodel-0.2.15 spec/support/callbacks.rb
mongomodel-0.2.14 spec/support/callbacks.rb
mongomodel-0.2.13 spec/support/callbacks.rb
mongomodel-0.2.12 spec/support/callbacks.rb
mongomodel-0.2.11 spec/support/callbacks.rb
mongomodel-0.2.10 spec/support/callbacks.rb
mongomodel-0.2.9 spec/support/callbacks.rb
mongomodel-0.2.8 spec/support/callbacks.rb
mongomodel-0.2.7 spec/support/callbacks.rb
mongomodel-0.2.6 spec/support/callbacks.rb
mongomodel-0.2.5 spec/support/callbacks.rb
mongomodel-0.2.4 spec/support/callbacks.rb