Class: R2OAS::Hooks::Hook

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/r2-oas/hooks/hook.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.repositoryObject

Returns the value of attribute repository.



13
14
15
# File 'lib/r2-oas/hooks/hook.rb', line 13

def repository
  @repository
end

Class Method Details

.execute_hook(on, *data, target_class) ⇒ Object



54
55
56
57
58
# File 'lib/r2-oas/hooks/hook.rb', line 54

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) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/r2-oas/hooks/hook.rb', line 60

def has_hook?(name, target_class)
  !!get_hook(name, target_class)
end

.off(uid, target_class) ⇒ Object

MEMO: Do not Use



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/r2-oas/hooks/hook.rb', line 36

def off(uid, target_class)
  target_repository = @repository[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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/r2-oas/hooks/hook.rb', line 22

def on(on, callback, target_class, once = false)
  target_repository = @repository[target_class]
  uid = target_repository.last_hook_id + 1
  target_repository.last_hook_id = uid

  @repository[target_class].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(target_class) ⇒ Object



15
16
17
18
19
20
# File 'lib/r2-oas/hooks/hook.rb', line 15

def register(target_class)
  @repository ||= {}
  @hooks ||= {}
  @repository[target_class] = Repository.new(target_class)
  self
end