Skip to content

Commit

Permalink
added new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
shawn42 committed Sep 27, 2011
1 parent 632ec57 commit e3ed0b5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Manifest.txt
@@ -1,6 +1,6 @@
History.txt History.txt
Manifest.txt Manifest.txt
README.txt README.rdoc
Rakefile Rakefile
lib/publisher.rb lib/publisher.rb
test/publisher_test.rb test/publisher_test.rb
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -21,8 +21,8 @@ Hoe.new('publisher', Publisher::VERSION) do |p|
p.author = 'Atomic Object' p.author = 'Atomic Object'
p.email = 'dev@atomicobject.com' p.email = 'dev@atomicobject.com'
p.summary = 'Event subscription and firing mechanism' p.summary = 'Event subscription and firing mechanism'
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n") p.description = p.paragraphs_of('README.rdoc', 2..5).join("\n\n")
p.url = p.paragraphs_of('README.txt', 1).first.gsub(/\* /,'').split(/\n/) p.url = p.paragraphs_of('README.rdoc', 1).first.gsub(/\* /,'').split(/\n/)
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n") p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
end end


Expand Down
24 changes: 19 additions & 5 deletions lib/publisher.rb
Expand Up @@ -42,12 +42,16 @@ module InstanceMethods
# Sign up a code block to be executed when an event is fired. # Sign up a code block to be executed when an event is fired.
# It's important to know the signature of the event, as your proc needs # It's important to know the signature of the event, as your proc needs
# to accept incoming parameters accordingly. # to accept incoming parameters accordingly.
def subscribe(event, &block) def subscribe(event, target=nil, callback=nil, &block)
ensure_valid event ensure_valid event
@subscriptions ||= {} @subscriptions ||= {}
listeners = @subscriptions[event] listeners = @subscriptions[event]
listeners ||= [] listeners ||= []
listeners << block if target && callback
listeners << [target, callback]
else
listeners << block
end
@subscriptions[event] = listeners @subscriptions[event] = listeners
end end
alias :when :subscribe alias :when :subscribe
Expand All @@ -59,8 +63,12 @@ def subscribe(event, &block)
def unsubscribe(event, listener) def unsubscribe(event, listener)
ensure_valid event ensure_valid event
if @subscriptions && @subscriptions[event] if @subscriptions && @subscriptions[event]
@subscriptions[event].delete_if do |block| @subscriptions[event].delete_if do |block_or_target|
eval('self',block.binding).equal?(listener) if block_or_target.is_a? Proc
eval('self',block_or_target.binding).equal?(listener)
else
block_or_target[0] == listener
end
end end
end end
end end
Expand All @@ -70,7 +78,13 @@ def unsubscribe(event, listener)
def fire(event, *args) #:nod def fire(event, *args) #:nod
ensure_valid event ensure_valid event
listeners = @subscriptions[event] if @subscriptions listeners = @subscriptions[event] if @subscriptions
listeners.each do |l| l.call(*args) end if listeners listeners.each do |l|
if l.is_a? Array
l[0].send(l[1],*args)
else
l.call(*args)
end
end if listeners
end end
alias :emit :fire alias :emit :fire
alias :notify :fire alias :notify :fire
Expand Down
29 changes: 29 additions & 0 deletions test/publisher_test.rb
Expand Up @@ -284,11 +284,32 @@ def test_subclasses_inherit_wide_open_events


assert_equal [ "surprise", "hooray" ], a assert_equal [ "surprise", "hooray" ], a
end end

def test_new_subscribe_syntax
@args = nil
shawn = Shawn.new
shawn.when :boom, self, :do_it
shawn.go(:a, :b)
assert_equal @args, [:a, :b]
end

def test_new_unsubscribe_syntax
@args = nil
shawn = Shawn.new
shawn.when :boom, self, :do_it
shawn.unsubscribe :boom, self
shawn.go(:a, :b)
assert_nil @args
end


# #
# HELPERS # HELPERS
# #


def do_it(*args)
@args = args
end

class SomethingWatcher class SomethingWatcher
attr_reader :observations attr_reader :observations
def initialize(something) def initialize(something)
Expand Down Expand Up @@ -425,4 +446,12 @@ def kick_it(sym)
end end
end end


class Shawn
extend Publisher
can_fire :boom
def go(*args)
fire :boom, *args
end
end

end end

0 comments on commit e3ed0b5

Please sign in to comment.