Skip to content

Commit

Permalink
Merge 10ea2a1 into 7f5d934
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoffner committed Mar 1, 2014
2 parents 7f5d934 + 10ea2a1 commit f2ee8d7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/wisper/registration/object.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
module Wisper
class ObjectRegistration < Registration
attr_reader :with, :prefix, :allowed_classes
attr_reader :with, :prefix, :allowed_classes, :allow_private

def initialize(listener, options)
super(listener, options)
@with = options[:with]
@prefix = stringify_prefix(options[:prefix])
@allowed_classes = Array(options[:scope]).map(&:to_s).to_set
@allow_private = options[:allow_private] || options[:private]
fail_on_async if options.has_key?(:async)
end

def broadcast(event, publisher, *args)
method_to_call = map_event_to_method(event)
if should_broadcast?(event) && listener.respond_to?(method_to_call) && publisher_in_scope?(publisher)
listener.public_send(method_to_call, *args)
if should_broadcast?(event) && listener.respond_to?(method_to_call, allow_private) && publisher_in_scope?(publisher)
if allow_private
listener.send(method_to_call, *args)
else
listener.public_send(method_to_call, *args)
end
end
end

Expand Down
37 changes: 37 additions & 0 deletions lib/wisper/rspec/matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'rspec/expectations'

module WisperMatchers
class ShouldPublish
def initialize(publisher, event)
@publisher = publisher
@event = event
end

def matches?(block)
published = false
@publisher.on(@event) { published = true }

block.call

published
end

def failure_message_for_should
"expected #{@publisher.class.name} to broadcast #{@event} event"
end

def failure_message_for_should_not
"expected #{@publisher.class.name} not to broadcast #{@event} event"
end
end

def publish_event(publisher, event)
ShouldPublish.new(publisher, event)
end

alias broadcast publish_event
end

RSpec::configure do |config|
config.include(WisperMatchers)
end
31 changes: 31 additions & 0 deletions spec/lib/wisper/publisher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'spec_helper'



describe Wisper::Publisher do
let(:listener) { double('listener') }
let(:publisher) { publisher_class.new }
Expand Down Expand Up @@ -76,6 +78,35 @@
end
end

class PrivateListener
def happened?
@happened ||= false
end

private

def it_happened
@happened = true
end
end

describe ':allow_private argument' do
let(:listener) { PrivateListener.new }

it 'allows private listener methods to be called when true' do
publisher.add_listener(listener, private: true)
publisher.send(:broadcast, 'it_happened')
listener.happened?.should be_true
end

it 'ignores private listener methods when false' do
publisher.add_listener(listener)
publisher.send(:broadcast, 'it_happened')
listener.happened?.should be_false
end

end

# NOTE: these are not realistic use cases, since you would only ever use
# `scope` when globally subscribing.
describe ':scope argument' do
Expand Down

0 comments on commit f2ee8d7

Please sign in to comment.