Sha256: a07752d9f3f988d246c913a18853d2c74798298bebc992644097e4da7b649e9f

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module PassiveRecord
  module Hooks
    class Hook
      attr_reader :kind

      def initialize(kind,*meth_syms,&blk)
        @kind = kind
        @methods_to_call = meth_syms
        @block_to_invoke = blk
      end

      def run(instance)
        @methods_to_call.each do |meth|
          instance.send(meth)
        end

        unless @block_to_invoke.nil?
          instance.instance_eval(&@block_to_invoke)
        end
      end
    end

    def before_create_hooks
      @hooks ||= []
      @hooks.select { |hook| hook.kind == :before_create }
    end

    def before_create(*meth_syms, &blk)
      hook = Hook.new(:before_create,*meth_syms,&blk)
      @hooks ||= []
      @hooks += [ hook ]
    end

    def after_create_hooks
      @hooks ||= []
      @hooks.select { |hook| hook.kind == :after_create }
    end

    def after_create(*meth_syms, &blk)
      hook = Hook.new(:after_create,*meth_syms,&blk)
      @hooks ||= []
      @hooks += [ hook ]
      self
    end

    def after_update_hooks
      @hooks ||= []
      @hooks.select { |hook| hook.kind == :after_update }
    end

    def after_update(*meth_syms, &blk)
      hook = Hook.new(:after_update,*meth_syms,&blk)
      @hooks ||= []
      @hooks += [ hook ]
      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
passive_record-0.3.1 lib/passive_record/hooks.rb