Class: Arkenstone::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/arkenstone/network/hook.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_hooks_for_class(klass) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/arkenstone/network/hook.rb', line 29

def all_hooks_for_class(klass)
  all_hooks = []
  if klass.arkenstone_inherit_hooks
    klass.ancestors.each do |ancestor|
      break if     ancestor == Arkenstone::Associations::InstanceMethods
      break unless ancestor.respond_to?(:arkenstone_hooks)

      all_hooks.concat ancestor.arkenstone_hooks unless ancestor.arkenstone_hooks.nil?
    end
  else
    all_hooks = klass.arkenstone_hooks
  end
  all_hooks
end

.call_error_hooks(klass, response) ⇒ Object

Calls all of the available ‘on_error` hooks available for the class.



25
26
27
# File 'lib/arkenstone/network/hook.rb', line 25

def call_error_hooks(klass, response)
  call_hook klass, proc { |h| h.on_error response }
end

.call_hook(klass, enumerator) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/arkenstone/network/hook.rb', line 57

def call_hook(klass, enumerator)
  hooks = []
  if klass.arkenstone_inherit_hooks == true
    klass.ancestors.each do |ancestor|
      break if ancestor == Arkenstone::Associations::InstanceMethods

      if ancestor.respond_to?(:arkenstone_hooks) && !ancestor.arkenstone_hooks.nil?
        hooks.concat ancestor.arkenstone_hooks
      end
    end
  else
    hooks = klass.arkenstone_hooks
  end
  hooks&.each(&enumerator)
end

.call_request_hooks(klass, request) ⇒ Object

Calls all of the available ‘before_request` hooks available for the class.



15
16
17
# File 'lib/arkenstone/network/hook.rb', line 15

def call_request_hooks(klass, request)
  call_hook klass, proc { |h| h.before_request request }
end

.call_response_hooks(klass, response) ⇒ Object

Calls all of the available ‘after_complete` hooks available for the class.



20
21
22
# File 'lib/arkenstone/network/hook.rb', line 20

def call_response_hooks(klass, response)
  call_hook klass, proc { |h| h.after_complete response }
end

.has_hooks?(klass) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# File 'lib/arkenstone/network/hook.rb', line 44

def has_hooks?(klass)
  return true if klass_has_hooks? klass
  if klass.respond_to?(:arkenstone_inherit_hooks) && klass.arkenstone_inherit_hooks
    return klass.ancestors.any? { |ancestor| klass_has_hooks? ancestor }
  end

  false
end

.klass_has_hooks?(klass) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/arkenstone/network/hook.rb', line 53

def klass_has_hooks?(klass)
  klass.respond_to?(:arkenstone_hooks) and klass.arkenstone_hooks and klass.arkenstone_hooks.count.positive?
end

Instance Method Details

#after_complete(response) ⇒ Object



7
# File 'lib/arkenstone/network/hook.rb', line 7

def after_complete(response); end

#before_request(env) ⇒ Object



5
# File 'lib/arkenstone/network/hook.rb', line 5

def before_request(env); end

#encode_attributes(attributes) ⇒ Object



9
# File 'lib/arkenstone/network/hook.rb', line 9

def encode_attributes(attributes); end

#on_error(response) ⇒ Object



11
# File 'lib/arkenstone/network/hook.rb', line 11

def on_error(response); end