Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updated to work with ruby 1.8 and 1.9
  • Loading branch information
schleyfox authored and technicalpickles committed Jul 7, 2011
1 parent 3ccbc2c commit 141ab8a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
pkg
*.swp
27 changes: 21 additions & 6 deletions lib/hookr.rb
@@ -1,5 +1,12 @@
require 'set'
require 'generator'

if RUBY_VERSION =~ /^1.9.\d+$/
require 'enumerator'
MyEnumerator = Enumerator
else
require 'generator'
MyEnumerator = Generator
end
require 'rubygems'
require 'fail_fast'

Expand Down Expand Up @@ -254,14 +261,14 @@ def execute_hook_iteratively(hook_name, event)
fetch_or_create_hooks[hook_name].execute_callbacks(event)
end

# Returns a Generator which yields:
# Returns a Enumerator which yields:
# 1. Wildcard callbacks, in reverse order, followed by
# 2. +hook_name+ callbacks, in reverse order, followed by
# 3. a proc which delegates to +block+
#
# Intended for use with recursive hook execution.
def callback_generator(hook_name, block)
Generator.new do |g|
MyEnumerator.new do |g|
fetch_or_create_hooks[:__wildcard__].each_callback_reverse do |callback|
g.yield callback
end
Expand Down Expand Up @@ -508,6 +515,14 @@ def [](index)
end
end

def to_a
if frozen? && !@keys
@hash.keys.sort
else
super
end
end

# get the first callback
def first
each do |cb|
Expand All @@ -521,7 +536,7 @@ def each_reverse(&block)

end

Callback = Struct.new(:handle, :index) do
class Callback < Struct.new(:handle, :index)
include Comparable
include FailFast::Assertions

Expand Down Expand Up @@ -646,8 +661,8 @@ def next(*args)
assert(recursive, callbacks)
event = self.class.new(source, name, arguments, recursive, callbacks)
event.arguments = args unless args.empty?
if callbacks.next?
callbacks.next.call(event)
if c = callbacks.next
c.call(event)
else
raise "No more callbacks!"
end
Expand Down
2 changes: 1 addition & 1 deletion spec/hookr_spec.rb
Expand Up @@ -173,7 +173,7 @@ def self.make_hook(name, parent, params)

it "should define a Listener class responding to #foo and #bar" do
@listener_class = @class::Listener
@listener_class.instance_methods(false).should include("foo", "bar")
@listener_class.instance_methods(false).map(&:to_s).should include("foo", "bar")
end

describe "given a subscribed Listener" do
Expand Down

0 comments on commit 141ab8a

Please sign in to comment.