Sha256: 4d86590cd49ad5aef1c0efce94bda71c80733bf68efa0d90820c60662bf7ba10

Contents?: true

Size: 672 Bytes

Versions: 6

Compression:

Stored size: 672 Bytes

Contents

require 'facets/kernel/instance_exec'

# TODO: hooks should be an inheritor
#
module Hook

  def self.append_features(base)
    base.extend self
  end

  def hooks
    @hooks ||= Hash.new{ |h,k| h[k] = [] }
  end

  def hook(name)
    name = name.to_sym

    (class << self; self; end).class_eval %{
      def #{name}(meth=nil, &blk)
        hooks[:#{name}] << (meth || blk)
      end
    }

    module_eval %{
      def #{name}(*args)
        self.class.hooks[:#{name}].each do |blk|
          if Proc === blk
            instance_exec(:#{name}, *args, &blk)
          else
            __send__(blk, :#{name}, *args)
          end
        end
      end
    }
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-2.8.4 lib/more/facets/hook.rb
facets-2.8.3 lib/more/facets/hook.rb
facets-2.8.2 lib/more/facets/hook.rb
facets-2.8.1 lib/more/facets/hook.rb
facets-2.8.0 lib/more/facets/hook.rb
facets-2.7.0 lib/more/facets/hook.rb