class GoogleAdsSavon::Hooks::Group

GoogleAdsSavon::Hooks::Group

Manages a list of hooks.

Public Class Methods

new(hooks = []) click to toggle source

Accepts an Array of hooks to start with.

# File lib/ads_savon/hooks/group.rb, line 12
def initialize(hooks = [])
  @hooks = hooks
end

Public Instance Methods

call(*args) click to toggle source

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

# File lib/ads_savon/hooks/group.rb, line 51
def call(*args)
  hooks.inject(nil) { |memo, hook| hook.call(*args) }
end
count() click to toggle source

Returns the number of hooks in this group.

# File lib/ads_savon/hooks/group.rb, line 22
def count
  hooks.count
end
define(id, hook, &block) click to toggle source

Adds a new hook.

# File lib/ads_savon/hooks/group.rb, line 27
def define(id, hook, &block)
  hooks << Hook.new(id, hook, &block)
end
empty?() click to toggle source

Returns whether this group contains hooks.

# File lib/ads_savon/hooks/group.rb, line 17
def empty?
  hooks.empty?
end
fire(hook, *args, &callback) click to toggle source

Fire a given hook with any given args.

# File lib/ads_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) click to toggle source

Removes hooks matching the given ids.

# File lib/ads_savon/hooks/group.rb, line 32
def reject(*ids)
  ids = ids.flatten
  hooks.reject! { |hook| ids.include? hook.id }
end

Private Instance Methods

hooks() click to toggle source
# File lib/ads_savon/hooks/group.rb, line 57
def hooks
  @hooks ||= []
end
select(hook) click to toggle source

Returns a new group for a given hook.

# File lib/ads_savon/hooks/group.rb, line 62
def select(hook)
  Group.new hooks.select { |h| h.hook == hook }
end