Permalink
Browse files

Extracted Broadcast::Message into Broadcast::Publishable to allow mix…

…ins and making any class a message
  • Loading branch information...
Marcin Bunsch Marcin Bunsch
Marcin Bunsch authored and Marcin Bunsch committed Jun 7, 2011
1 parent aadbba0 commit 9e99dfbcaa02de09ba260e5674a2ed4f28b6d098
Showing with 45 additions and 31 deletions.
  1. +2 −0 lib/broadcast.rb
  2. +1 −31 lib/broadcast/message.rb
  3. +42 −0 lib/broadcast/publishable.rb
View
@@ -30,5 +30,7 @@ def self.setup(&block)
end
+require 'broadcast/publishable'
require 'broadcast/medium'
require 'broadcast/message'
+
View
@@ -1,35 +1,5 @@
class Broadcast::Message
-
autoload "Simple", "broadcast/simple"
-
- attr_accessor :options
- class << self
- attr_accessor :media
- def medium(name, options = {})
- self.media ||= []
- 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
- Broadcast::Medium.const_get(medium[:name].to_s.downcase.capitalize).new(medium[:options]).publish(self)
- rescue
- Broadcast.logger.error "Publishing of #{self.class.name} to #{medium[:name]} failed:\n#{$!}"
- end
- end
- end
-
- def subject
- end
-
- def body
- ""
- end
+ include Broadcast::Publishable
end
@@ -0,0 +1,42 @@
+# 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
+
+ module ClassMethods
+
+ attr_accessor :media
+
+ def medium(name, options = {})
+ self.media ||= []
+ 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
+ Broadcast::Medium.const_get(medium[:name].to_s.downcase.capitalize).new(medium[:options]).publish(self)
+ rescue
+ Broadcast.logger.error "Publishing of #{self.class.name} to #{medium[:name]} failed:\n#{$!}"
+ end
+ end
+ end
+
+ def subject
+ end
+
+ def body
+ ""
+ end
+
+end

0 comments on commit 9e99dfb

Please sign in to comment.