Sha256: 3a34dce56e80cb61451d90d3abcacea6add3eeefab5395370864c0cfee782d89

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module Hook
  def before(*meth_names, &callback)
    meth_names.each{|meth_name| add_hook :before, meth_name, &callback }
  end
  
  def after(*meth_names, &callback)
    meth_names.each{|meth_name| add_hook :after, meth_name, &callback }
  end
  
  def hooks
    @hooks ||= Hash.new do |hash, method_name|
      hash[method_name] = { before: [], after: [], hijacked: false }
    end
  end
  
  def add_hook(where, meth_name, &callback)
    hooks[meth_name][where] << callback
    ensure_hijacked meth_name
  end
  
  def method_added(meth_name)
    ensure_hijacked meth_name if hooks.has_key? meth_name
  end
  
  def ensure_hijacked(meth_name)
    return if hooks[meth_name][:hijacked] || !instance_methods.include?(meth_name)
    meth = instance_method meth_name
    _hooks = hooks
    _hooks[meth_name][:hijacked] = true
    define_method meth_name do |*args, &block|
      _hooks[meth_name][:before].each do |callback|
        self.instance_exec(&callback)
      end
      return_value = meth.bind(self).call *args, &block
      _hooks[meth_name][:after].each do |callback|
        self.instance_exec(&callback)
      end
      return_value
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fastdfs-client-0.0.2 lib/fastdfs-client/hook.rb