Sha256: 0f2bab37a8635377c2d51dd18f72d93abbe8e2210da7f4e92ffa574df07f6331

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

require "savon/hooks/hook"

module Savon
  module Hooks

    # = Savon::Hooks::Group
    #
    # Manages a list of hooks.
    class Group

      # Accepts an Array of +hooks+ to start with.
      def initialize(hooks = [])
        @hooks = hooks
      end

      # Returns whether this group contains hooks.
      def empty?
        hooks.empty?
      end

      # Returns the number of hooks in this group.
      def count
        hooks.count
      end

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

      # Removes hooks matching the given +ids+.
      def reject(*ids)
        ids = ids.flatten
        hooks.reject! { |hook| ids.include? hook.id }
      end

      # Fire a given +hook+ with any given +args+.
      def fire(hook, *args, &callback)
        callable = select(hook)

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

      # Calls the hooks with the given +args+ and returns the
      # value of the last hooks.
      def call(*args)
        hooks.inject(nil) { |memo, hook| hook.call(*args) }
      end

      private

      def hooks
        @hooks ||= []
      end

      # Returns a new group for a given +hook+.
      def select(hook)
        Group.new hooks.select { |h| h.hook == hook }
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
savon-1.2.0 lib/savon/hooks/group.rb
savon-1.1.0 lib/savon/hooks/group.rb
savon-1.0.0 lib/savon/hooks/group.rb