Permalink
Browse files

Made the options Hashie::Mash a trait of Broadcast::Message. When Bro…

…adcast::Publishable is included to a class, we should assume that the class has it's own source of message body options.
  • Loading branch information...
1 parent 9e99dfb commit 257bda5595a8ea41c9a7fb5d769b5d1d39667ca4 Marcin Bunsch committed with Marcin Bunsch Jun 8, 2011
Showing with 62 additions and 8 deletions.
  1. +7 −0 lib/broadcast/message.rb
  2. +1 −8 lib/broadcast/publishable.rb
  3. +54 −0 spec/lib/broadcast/publishable_spec.rb
@@ -2,4 +2,11 @@ class Broadcast::Message
autoload "Simple", "broadcast/simple"
include Broadcast::Publishable
+ # Make the options Hashie::Mash a specific trait of Broadcast::Message
+ attr_accessor :options
+
+ def initialize(options = {})
+ @options = Hashie::Mash.new(options)
+ end
+
end
@@ -1,8 +1,6 @@
# Module which allows any class to be turned into a message
module Broadcast::Publishable
- attr_accessor :options
-
def self.included(base)
base.extend ClassMethods
end
@@ -12,16 +10,11 @@ module ClassMethods
attr_accessor :media
def medium(name, options = {})
- self.media ||= []
- self.media.push({ :name => name, :options => options })
+ (self.media ||= []).push({ :name => name, :options => options })
end
end
- def initialize(options = {})
- @options = Hashie::Mash.new(options)
- end
-
def publish
(self.class.media || []).each do |medium|
begin
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+class ClassToBecomeMessage
+ include Broadcast::Publishable
+
+ attr_accessor :something
+
+ def initialize(somthng)
+ self.something = somthng
+ end
+
+end
+
+describe Broadcast::Publishable do
+
+ describe "in class scope" do
+
+ it "should add the media accessor" do
+ ClassToBecomeMessage.methods.should include('media')
+ ClassToBecomeMessage.methods.should include('media=')
+ end
+
+ it "should add the medium method" do
+ ClassToBecomeMessage.methods.should include('medium')
+ end
+
+ end
+
+ describe "in instance scope" do
+
+ it "should add the publish method" do
+ ClassToBecomeMessage.public_instance_methods.should include('publish')
+ end
+
+ it "should add the subject method" do
+ ClassToBecomeMessage.public_instance_methods.should include('subject')
+ end
+
+ it "should add the body method" do
+ ClassToBecomeMessage.public_instance_methods.should include('body')
+ end
+
+ it "should not add the options accessor" do
+ ClassToBecomeMessage.public_instance_methods.should_not include('options')
+ ClassToBecomeMessage.public_instance_methods.should_not include('options=')
+ end
+
+ it "should not mess with the initializer" do
+ ClassToBecomeMessage.new(123).something.should == 123
+ end
+
+ end
+
+end

0 comments on commit 257bda5

Please sign in to comment.