Class: Savon::Hooks::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/savon/hooks/group.rb

Overview

= Savon::Hooks::Group

Manages a list of hooks.

Instance Method Summary collapse

Constructor Details

#initialize(hooks = []) ⇒ Group

Accepts an Array of +hooks+ to start with.



12
13
14
# File 'lib/savon/hooks/group.rb', line 12

def initialize(hooks = [])
  @hooks = hooks
end

Instance Method Details

#call(*args) ⇒ Object

Calls the hooks with the given +args+ and returns the value of the last hooks.



51
52
53
# File 'lib/savon/hooks/group.rb', line 51

def call(*args)
  hooks.inject(nil) { |memo, hook| hook.call(*args) }
end

#countObject

Returns the number of hooks in this group.



22
23
24
# File 'lib/savon/hooks/group.rb', line 22

def count
  hooks.count
end

#define(id, hook, &block) ⇒ Object

Adds a new hook.



27
28
29
# File 'lib/savon/hooks/group.rb', line 27

def define(id, hook, &block)
  hooks << Hook.new(id, hook, &block)
end

#empty?Boolean

Returns whether this group contains hooks.

Returns:

  • (Boolean)


17
18
19
# File 'lib/savon/hooks/group.rb', line 17

def empty?
  hooks.empty?
end

#fire(hook, *args, &callback) ⇒ Object

Fire a given +hook+ with any given +args+.



38
39
40
41
42
43
44
45
46
47
# File 'lib/savon/hooks/group.rb', line 38

def fire(hook, *args, &callback)
  callable = select(hook)

  if callable.empty?
    callback.call
  else
    args.unshift(callback) if callback
    callable.call(*args)
  end
end

#reject(*ids) ⇒ Object

Removes hooks matching the given +ids+.



32
33
34
35
# File 'lib/savon/hooks/group.rb', line 32

def reject(*ids)
  ids = ids.flatten
  hooks.reject! { |hook| ids.include? hook.id }
end