Sha256: 62fc3a7fc4d2f6d44c3f60a75761754fcf4ccc412ffe3cdf71dc8d7bf301ac88

Contents?: true

Size: 1007 Bytes

Versions: 3

Compression:

Stored size: 1007 Bytes

Contents

module ActionCallback
  private

  def initialize_callback_chain(mod)
    mod.instance_variable_set('@_callback_chain', Callback::Chain.new)

    mod.define_singleton_method(:_callback_chain) do
      instance_variable_get('@_callback_chain')
    end
  end

  def define_callback(callback_hook)
    define_method("#{callback_hook}_action") do |callback, method_scope|
      method_scope[:on].each do |mth_name|
        @_callback_chain.append_callback(callback_hook, mth_name, callback)

        undef_method(mth_name) if method_defined?(mth_name)

        class_eval <<-RUBY
          module ActionWithCallbacks
            define_method(:#{mth_name}) do |*args, &block|
              self.class._callback_chain.before_chain_of(:#{mth_name}).each { |cb| send(cb) }
              super(*args, &block)
              self.class._callback_chain.after_chain_of(:#{mth_name}).each { |cb| send(cb) }
            end
          end
        RUBY

        prepend self::ActionWithCallbacks
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
action_callback-0.2.1 lib/action_callback/define_callback.rb
action_callback-0.2.0 lib/action_callback/define_callback.rb
action_callback-0.1.0 lib/action_callback/define_callback.rb