Class: MethodFound::Builder

Inherits:
Module
  • Object
show all
Defined in:
lib/method_found/builder.rb

Overview

Creates set of interceptors to include into a class.

Examples:

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!!!"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize { ... } ⇒ Builder

Returns a new instance of Builder.

Yields:

  • Yields builder as context to block, to allow calling builder methods to create interceptors in included class.



30
31
32
33
# File 'lib/method_found/builder.rb', line 30

def initialize
  @interceptors = []
  super
end

Instance Attribute Details

#interceptorsObject (readonly)

Returns the value of attribute interceptors.



26
27
28
# File 'lib/method_found/builder.rb', line 26

def interceptors
  @interceptors
end

Instance Method Details

#intercept(*args, &block) ⇒ Object



35
36
37
38
# File 'lib/method_found/builder.rb', line 35

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