Module: Performify::Callbacks

Included in:
Base
Defined in:
lib/performify/callbacks.rb

Defined Under Namespace

Classes: UnknownTypeOfCallbackError

Constant Summary collapse

TYPES_OF_CALLBACK =
i[success fail].freeze

Instance Method Summary collapse

Instance Method Details

#clean_callbacksObject



7
8
9
# File 'lib/performify/callbacks.rb', line 7

def clean_callbacks
  @service_callbacks = {}
end

#execute_callbacks(type_of_callback, instance) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/performify/callbacks.rb', line 22

def execute_callbacks(type_of_callback, instance)
  unless TYPES_OF_CALLBACK.include?(type_of_callback)
    raise UnknownTypeOfCallbackError, "Type #{type_of_callback} is not allowed"
  end
  cbs = (@service_callbacks || {}).fetch(type_of_callback, [])
  cbs.each { |cb| cb.is_a?(Proc) ? instance.instance_eval(&cb) : instance.send(cb) }
  nil
end

#register_callback(type_of_callback, method_name = nil, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/performify/callbacks.rb', line 11

def register_callback(type_of_callback, method_name = nil, &block)
  unless TYPES_OF_CALLBACK.include?(type_of_callback)
    raise UnknownTypeOfCallbackError, "Type #{type_of_callback} is not allowed"
  end
  @service_callbacks ||= {}
  @service_callbacks[type_of_callback] ||= []
  @service_callbacks[type_of_callback] << method_name if method_name
  @service_callbacks[type_of_callback] << block if block_given?
  nil
end