Module: Dependor::Sinatra::Enabler

Included in:
Sinatra::Base
Defined in:
lib/dependor-sinatra/enabler.rb

Instance Method Summary collapse

Instance Method Details

#enable_dependor_for_method(method) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dependor-sinatra/enabler.rb', line 4

def enable_dependor_for_method(method)
  without_dependor = :"#{method}_without_dependor"

  # the only way to alias a private method
  instance_eval("alias #{without_dependor} #{method}")

  define_singleton_method(method) do |*args, &block|
    return __send__(without_dependor, *args, &block) if dependor_disabled?

    wrapped_block = Dependor::Sinatra::BlockWrapper.wrap(block)
    __send__(without_dependor, *args) do
      instance_eval(&wrapped_block)
    end
  end
end