Module: AroundMethod::Core

Included in:
Object
Defined in:
lib/around_method/core.rb

Instance Method Summary collapse

Instance Method Details

#apply_around_method(method_name, wrapper_method) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/around_method/core.rb', line 8

def apply_around_method(method_name, wrapper_method)
  renamed_original_method = "#{method_name}_inner"
  alias_method(renamed_original_method, method_name)
  with_method_added_hook_disabled do
    define_method(method_name) do
      original_method_proc = method(renamed_original_method)
      method(wrapper_method).call(&original_method_proc)
    end
  end
end

#around_method(method_name, wrapper_method) ⇒ Object



3
4
5
6
# File 'lib/around_method/core.rb', line 3

def around_method(method_name, wrapper_method)
  apply_around_method(method_name, wrapper_method) if method_defined?(method_name)
  around_method_wrapped[method_name] = wrapper_method
end

#around_method_wrappedObject



26
27
28
# File 'lib/around_method/core.rb', line 26

def around_method_wrapped
  @around_method_wrapped ||= {}
end

#disable_method_added_hook?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/around_method/core.rb', line 30

def disable_method_added_hook?
  @disable_method_added_hook
end

#enable_method_added_hook?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/around_method/core.rb', line 34

def enable_method_added_hook?
  !disable_method_added_hook?
end

#method_added(method_name) ⇒ Object



19
20
21
22
23
24
# File 'lib/around_method/core.rb', line 19

def method_added(method_name)
  if around_method_wrapped.has_key?(method_name) && enable_method_added_hook?
    apply_around_method(method_name, around_method_wrapped[method_name])
  end
  super
end

#with_method_added_hook_disabledObject

Allow AroundMethod’s method_added hook to be temporarily disabled, otherwise application will trigger an infinite loop.



40
41
42
43
44
# File 'lib/around_method/core.rb', line 40

def with_method_added_hook_disabled
  @disable_method_added_hook = true
  yield
  @disable_method_added_hook = false
end