Sha256: 14119fba3b0c89515e840e23943c42ae8d9859709694f2d575c2244ea70d7c52

Contents?: true

Size: 1006 Bytes

Versions: 1

Compression:

Stored size: 1006 Bytes

Contents

$:.unshift File.dirname(__FILE__)
require 'ext/instance_exec'
require 'method_matching/extendable_block'

module Kernel
  def method_matching(regex, &definition)
    method_matchers[regex] = MethodMatching::ExtendableBlock.new &definition
    klass = private_methods.include?("include") ? self : (class << self; self; end)
    klass.class_eval do
      include MethodMissingDefinition unless included_modules.include?(MethodMissingDefinition)
    end
  end

  def method_matchers
    @method_matchers ||= { }
  end

  module MethodMissingDefinition
    def method_missing(method_name, *args, &block)
      try_matcher = Proc.new do |matcher, mdef|
        if method_name.to_s =~ matcher
          mdef.block = block
          return mdef.call(method_name, *args)
        end
      end
      
      method_matchers.each { |m, mdef| try_matcher.call(m, mdef) }
      self.class.method_matchers.each { |m, mdef| try_matcher.call(m, mdef) }
      super
    end
  end
end

#Module.send :include, MethodMatching

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pat-maddox-method_matching-0.1.1 lib/method_matching.rb