Commit 1c3af97

mo khan <mo@mokhan.ca>
2013-12-08 01:45:22
extract event aggregator to publish events too.
1 parent cff34af
lib/presentation/presenters/application_shell_presenter.rb
@@ -1,13 +1,14 @@
 class ApplicationShellPresenter
-  def initialize(view)
+  def initialize(view, event_aggregator)
     @view = view
+    @event_aggregator = event_aggregator
   end
 
   def present
-    @view.set_title("Hello World")
     @view.set_title("Hello World")
     @view.signal_connect "destroy" do
-      Gtk.main_quit
+      puts "publishing halt to event aggregator"
+      @event_aggregator.publish(:halt)
     end
     @view.set_default_size 250, 200
     @view.set_window_position Gtk::Window::POS_CENTER
lib/presentation/windows/application_shell.rb
@@ -1,4 +1,6 @@
-class ApplicationShell < Gtk::Window
+require "window"
+
+class ApplicationShell < Window
   def initialize
     super
   end
lib/presentation/windows/window.rb
@@ -0,0 +1,7 @@
+require 'gtk2'
+
+class Window < Gtk::Window
+  def initialize
+    super
+  end
+end
lib/presentation/event_aggregator.rb
@@ -0,0 +1,26 @@
+class EventAggregator
+  def initialize(subscribers = {})
+    @subscribers = subscribers
+  end
+
+  def publish(event, *args)
+    subscribers = subscribers_for(event)
+    puts "all #{@subscribers}"
+    puts "found #{subscribers}"
+    subscribers.each do |subscriber|
+      puts "publishing #{event} to #{subscriber}"
+      subscriber.public_send(event, args)
+    end
+  end
+
+  def subscribe(event, subscriber)
+    @subscribers[event] = [] unless @subscribers.key?(event)
+    @subscribers[event].push(subscriber)
+  end
+
+  private
+
+  def subscribers_for(event)
+    @subscribers[event]
+  end
+end
lib/application.rb
@@ -1,11 +1,21 @@
-require 'gtk2'
-require 'presentation/windows/application_shell'
-require 'presentation/presenters/application_shell_presenter'
+Dir["**/**/*.rb"].each { |file| $:.unshift(File.dirname(file)) }
+
+require 'application_shell_presenter'
+require 'application_shell'
+require 'event_aggregator'
+
+class ShutdownCommand
+  def halt(*message)
+    Gtk.main_quit
+  end
+end
 
 class Application
   def run(arguments)
     Gtk.init
-    presenter = ApplicationShellPresenter.new(ApplicationShell.new)
+    event_aggregator = EventAggregator.new
+    event_aggregator.subscribe(:halt, ShutdownCommand.new)
+    presenter = ApplicationShellPresenter.new(ApplicationShell.new, event_aggregator)
     presenter.present
     Gtk.main
   end