Sha256: 5368c28ef9caed21790d5cc230796bce82fa6479dc724364e1ba374b8ba97bc0

Contents?: true

Size: 720 Bytes

Versions: 1

Compression:

Stored size: 720 Bytes

Contents

module MethodFound
  class Interceptor < Module
    def initialize(regex, &intercept_block)
      define_method :method_missing do |method_name, *arguments, &method_block|
        if matches = regex.match(method_name)
          instance_exec(method_name, matches, *arguments, &intercept_block)
        else
          super(method_name, *arguments, &method_block)
        end
      end

      define_method :respond_to_missing? do |method_name, include_private = false|
        (method_name =~ regex) || super(method_name, include_private)
      end

      define_method :inspect do
        klass = self.class
        name  = klass.name || klass.inspect
        "#<#{name}: #{regex.inspect}>"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
method_found-0.1.1 lib/method_found/interceptor.rb