Class: Kudzu::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/kudzu/callback.rb

Constant Summary collapse

CALLBACKS =

2xx

[:on_success,      # 2xx
:on_redirection,  # 3xx
:on_client_error, # 4xx
:on_server_error, # 5xx
:on_filter,       # 2xx, filtered
:on_failure,      # Exception
:before_enqueue, 
:after_enqueue,
:before_fetch, 
:after_fetch,
:before_register,
:after_register,
:before_delete,
:after_delete,
]

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Callback

Returns a new instance of Callback.


19
20
21
22
# File 'lib/kudzu/callback.rb', line 19

def initialize(&block)
  @callback = {}
  instance_eval(&block) if block
end

Instance Method Details

#around(name, *args) ⇒ Object


35
36
37
38
39
40
41
# File 'lib/kudzu/callback.rb', line 35

def around(name, *args)
  before_name = "before_#{name}".to_sym
  after_name = "after_#{name}".to_sym
  @callback[before_name].call(*args) if @callback.key?(before_name)
  yield
  @callback[after_name].call(*args) if @callback.key?(after_name)
end

#on(name, *args) ⇒ Object


30
31
32
33
# File 'lib/kudzu/callback.rb', line 30

def on(name, *args)
  on_name = "on_#{name}".to_sym
  @callback[on_name].call(*args) if @callback.key?(on_name)
end