class R2OAS::Hooks::Hook

Attributes

repository[RW]

Public Class Methods

execute_hook(on, *data, target_class) click to toggle source
# File lib/r2-oas/hooks/hook.rb, line 56
def execute_hook(on, *data, target_class)
  return data unless has_hook?(on, target_class)

  execute_global_hook(on, *data, target_class)
end
has_hook?(name, target_class) click to toggle source
# File lib/r2-oas/hooks/hook.rb, line 62
def has_hook?(name, target_class)
  !!get_hook(name, target_class)
end
off(uid, target_class) click to toggle source

MEMO: Do not Use

# File lib/r2-oas/hooks/hook.rb, line 38
def off(uid, target_class)
  target_repository = @repository[@type][target_class]
  result = uid

  target_repository.global_hooks_data.each do |on|
    global_hooks = target_repository.global_hooks_data[on]
    index = global_hooks.find_index { |hook| hook.uid == uid }

    if index
      global_hooks.delete_if { |hook| hook.uid == uid }
    else
      result = nil
    end
  end

  result
end
on(on, callback, target_class, once = false) click to toggle source
# File lib/r2-oas/hooks/hook.rb, line 24
def on(on, callback, target_class, once = false)
  target_repository = @repository[@type][target_class]
  uid = target_repository.last_hook_id + 1
  target_repository.last_hook_id = uid

  target_repository.global_hooks_data[on] ||= []
  global_hook = GlobalHook.new(callback, once, uid, target_class)

  target_repository.global_hooks_data[on].push(global_hook)

  uid
end
register(type, target_class) click to toggle source
# File lib/r2-oas/hooks/hook.rb, line 15
def register(type, target_class)
  @repository ||= {}
  @repository[type] ||= {}
  @type = type
  @hooks ||= {}
  @repository[type][target_class] = Repository.new(target_class)
  self
end

Private Class Methods

execute_global_hook(on, *data, target_class) click to toggle source
# File lib/r2-oas/hooks/hook.rb, line 68
def execute_global_hook(on, *data, target_class)
  global_hook = get_hook(on, target_class)
  global_hook.present? ? global_hook.call(*data) : data
end
get_hook(name, target_class) click to toggle source
# File lib/r2-oas/hooks/hook.rb, line 73
def get_hook(name, target_class)
  target_class.hooks[name]&.first
end