Sha256: 36d329b1b83418402f3e25d6ced4cd2dccfee8fe236ba5a536fb2d809938898c

Contents?: true

Size: 887 Bytes

Versions: 2

Compression:

Stored size: 887 Bytes

Contents

module MethodFound
=begin

Creates set of interceptors to include into a class.

@example
  class Post
    builder = MethodFound::Builder.new {
      intercept /\Asay_([a-z]+)\Z/ do |method_name, matches, *arguments|
        "#{matches[1]}!"
      end

      intercept /\Ayell_([a-z]+)\Z/ do |method_name, matches, *arguments|
        "#{matches[1]}!!!"
      end
    }
  end

  foo = Foo.new
  foo.say_hello
  #=> "hello!"
  foo.yell_hello
  #=> "hello!!!"
=end
  class Builder < Module
    attr_reader :interceptors

    # @yield Yields builder as context to block, to allow calling builder
    #   methods to create interceptors in included class.
    def initialize(&block)
      @interceptors = []
      instance_eval &block
    end

    def intercept(*args, &block)
      @interceptors.push(interceptor = Interceptor.new(*args, &block))
      include interceptor
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
method_found-0.1.4 lib/method_found/builder.rb
method_found-0.1.3 lib/method_found/builder.rb