Commit 03d5b793 authored by Fabio Pelosin's avatar Fabio Pelosin

[HooksManager] Introduction

parent 4e084442
...@@ -40,6 +40,7 @@ module Pod ...@@ -40,6 +40,7 @@ module Pod
autoload :Executable, 'cocoapods/executable' autoload :Executable, 'cocoapods/executable'
autoload :ExternalSources, 'cocoapods/external_sources' autoload :ExternalSources, 'cocoapods/external_sources'
autoload :Installer, 'cocoapods/installer' autoload :Installer, 'cocoapods/installer'
autoload :HooksManager, 'cocoapods/hooks_manager'
autoload :PodTarget, 'cocoapods/target/pod_target' autoload :PodTarget, 'cocoapods/target/pod_target'
autoload :Project, 'cocoapods/project' autoload :Project, 'cocoapods/project'
autoload :Resolver, 'cocoapods/resolver' autoload :Resolver, 'cocoapods/resolver'
......
module Pod
# Provides support for the hook system of CocoaPods. The system is designed
# especially for plugins. Interested clients can register to notifications by
# name.
#
# The blocks, to prevent compatibility issues, will receive
# one and only one argument: a context object. This object should be simple
# storage of information (a typed hash). Notifications senders are
# responsible to indicate the class of the object associated with their
# notification name.
#
# Context object should not remove attribute accessors to not break
# compatibility with the plugins (this promise will be honoured strictly
# from CocoaPods 1.0).
#
module HooksManager
class << self
# @return [Hash{Symbol => Proc}] The list of the blocks that are
# registered for each notification name.
#
attr_reader :registrations
# Registers a block for the hook with the given name.
#
# @param [Symbol] name
# The name of the notification.
#
# @param [Proc] block
# The block.
#
def register(name, &block)
raise ArgumentError, 'Missing name' unless name
raise ArgumentError, 'Missing block' unless block
@registrations ||= Hash.new
@registrations[name] ||= Array.new
@registrations[name] << block
end
# Runs all the registered blocks for the hook with the given name.
#
# @param [Symbol] name
# The name of the hook.
#
# @param [Object] context
# The context object which should be passed to the blocks.
#
def run(name, context)
raise ArgumentError, 'Missing name' unless name
raise ArgumentError, 'Missing options' unless context
if @registrations
blocks = @registrations[name]
if blocks
blocks.each do |block|
block.call(context)
end
end
end
end
end
end
end
require File.expand_path('../../spec_helper', __FILE__)
module Pod
describe HooksManager do
before do
@hooks_manager = Pod::HooksManager
end
describe 'register' do
it "allows to register a block for a notification with a given name" do
@hooks_manager.register(:post_install) {}
@hooks_manager.registrations[:post_install].count.should == 1
@hooks_manager.registrations[:post_install].first.class.should == Proc
end
it "raises if no name is given" do
should.raise ArgumentError do
@hooks_manager.register(nil) {}
end
end
it "raises if no block is given" do
should.raise ArgumentError do
@hooks_manager.register(:post_install)
end
end
end
describe 'run' do
it "invokes the hooks" do
@hooks_manager.register(:post_install) do |options|
true.should.be.true
end
@hooks_manager.run(:post_install, Object.new)
end
it "handles the case that no listeners have registered" do
should.not.raise do
@hooks_manager.run(:post_install, Object.new)
end
end
it "handles the case that no listeners have registered for a name" do
@hooks_manager.register(:post_install) do |options|
true.should.be.true
end
should.not.raise do
@hooks_manager.run(:pre_install, Object.new)
end
end
it "raises if no name is given" do
should.raise ArgumentError do
@hooks_manager.run(nil, Object.new) {}
end
end
it "raises if no context object is given" do
should.raise ArgumentError do
@hooks_manager.run(:post_install, nil)
end
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment