Sha256: 3dd0e71a75ebc8bfeabc8c5e3ab36e787fe5a30619992f03794e37b1b108a510

Contents?: true

Size: 1.9 KB

Versions: 26

Compression:

Stored size: 1.9 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

        instance
      end
    end

    def inject_hook(hook)
      @hooks ||= []
      @hooks += [ hook ]
    end

    def find_hooks_of_type(type)
      @hooks ||= []
      @hooks.select { |hook| hook.kind == type }
    end

    def before_create_hooks
      find_hooks_of_type :before_create
    end

    def before_create(*meth_syms, &blk)
      hook = Hook.new(:before_create,*meth_syms,&blk)
      inject_hook hook
      self
    end

    def after_create_hooks
      find_hooks_of_type :after_create
    end

    def after_create(*meth_syms, &blk)
      hook = Hook.new(:after_create,*meth_syms,&blk)
      inject_hook hook
      self
    end

    def before_update_hooks
      find_hooks_of_type :before_update
    end

    def before_update(*meth_syms, &blk)
      hook = Hook.new(:before_update,*meth_syms,&blk)
      inject_hook hook
      self
    end

    def after_update_hooks
      find_hooks_of_type :after_update
    end

    def after_update(*meth_syms, &blk)
      hook = Hook.new(:after_update,*meth_syms,&blk)
      inject_hook hook
      self
    end

    def before_destroy_hooks
      find_hooks_of_type :before_destroy
    end

    def before_destroy(*meth_syms,&blk)
      hook = Hook.new(:before_destroy,*meth_syms,&blk)
      inject_hook(hook)
      self
    end

    def after_destroy_hooks
      find_hooks_of_type :after_destroy
    end

    def after_destroy(*meth_syms,&blk)
      hook = Hook.new(:after_destroy,*meth_syms,&blk)
      inject_hook(hook)
      self
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
passive_record-0.4.15 lib/passive_record/hooks.rb
passive_record-0.4.14 lib/passive_record/hooks.rb
passive_record-0.4.13 lib/passive_record/hooks.rb
passive_record-0.4.12 lib/passive_record/hooks.rb
passive_record-0.4.11 lib/passive_record/hooks.rb
passive_record-0.4.10 lib/passive_record/hooks.rb
passive_record-0.4.8 lib/passive_record/hooks.rb
passive_record-0.4.7 lib/passive_record/hooks.rb
passive_record-0.4.6 lib/passive_record/hooks.rb
passive_record-0.4.5 lib/passive_record/hooks.rb
passive_record-0.4.4 lib/passive_record/hooks.rb
passive_record-0.4.3 lib/passive_record/hooks.rb
passive_record-0.4.2 lib/passive_record/hooks.rb
passive_record-0.4.1 lib/passive_record/hooks.rb
passive_record-0.4.0 lib/passive_record/hooks.rb
passive_record-0.3.22 lib/passive_record/hooks.rb
passive_record-0.3.21 lib/passive_record/hooks.rb
passive_record-0.3.20 lib/passive_record/hooks.rb
passive_record-0.3.19 lib/passive_record/hooks.rb
passive_record-0.3.18 lib/passive_record/hooks.rb