Skip to content

Commit

Permalink
you can now call respond_to_event :type without a :with option. defau…
Browse files Browse the repository at this point in the history
…lts to type. xmas gift for Thierry ;-)
  • Loading branch information
apotonick committed Dec 24, 2010
1 parent 0b68b5d commit 0f0824b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
41 changes: 19 additions & 22 deletions lib/apotomo/event_methods.rb
Expand Up @@ -2,19 +2,19 @@

module Apotomo
# Introduces event-processing functions into the StatefulWidget.

module EventMethods
extend ActiveSupport::Concern

included do
after_initialize :add_class_event_handlers
end

attr_writer :page_updates
# Replacement for the EventProcessor singleton queue.

def page_updates
@page_updates ||= []
end

def self.included(base)
base.extend(ClassMethods)
base.after_initialize :add_class_event_handlers
end

def add_class_event_handlers(*)
self.class.responds_to_event_options.each { |options| respond_to_event(*options) }
end
Expand All @@ -34,37 +34,34 @@ def responds_to_event_options
# state, which implies an update of the invoked widget.
#
# You may configure the event handler with the following <tt>options</tt>:
# :with => (required) the state to invoke on the target widget
# :with => (optional) the state to invoke on the target widget, defaults to +type+.
# :on => (optional) the targeted widget's id, defaults to <tt>self.name</tt>
# :from => (optional) the source id of the widget that triggered the event, defaults to any widget
#
# Example:
#
# trap = cell(:input_field, :smell_like_cheese, 'mouse_trap')
# trap = widget(:trap, :charged, 'mouse_trap')
# trap.respond_to_event :mouseOver, :with => :catch_mouse
#
# This would instruct <tt>trap</tt> to catch a <tt>:mouseOver</tt> event from any widget (including itself) and
# This would instruct +trap+ to catch a <tt>:mouseOver</tt> event from any widget (including itself) and
# to invoke the state <tt>:catch_mouse</tt> on itself as trigger.
#
#
# hunter = cell(:form, :hunt_for_mice, 'my_form')
# hunter << cell(:input_field, :smell_like_cheese, 'mouse_trap')
# hunter << cell(:text_area, :stick_like_honey, 'bear_trap')
# hunter = widget(:form, :hunt_for_mice, 'my_form')
# hunter << widget(:input_field, :smell_like_cheese, 'mouse_trap')
# hunter << widget(:text_area, :stick_like_honey, 'bear_trap')
# hunter.respond_to_event :captured, :from => 'mouse_trap', :with => :refill_cheese, :on => 'mouse_trap'
#
# As both the bear- and the mouse trap can trigger a <tt>:captured</tt> event the later <tt>respond_to_event</tt>
# would invoke <tt>:refill_cheese</tt> on the <tt>mouse_trap</tt> widget as soon as this and only this widget fired.
# It is important to understand the <tt>:from</tt> parameter as it filters the event source - it wouldn't make
# sense to refill the mouse trap if the bear trap snapped, would it?

def respond_to_event(type, options)
options[:once] = true if options[:once].nil?

handler_opts = {}
handler_opts[:widget_id] = options[:on] || self.name
handler_opts[:state] = options[:with]
# sense to refill the mouse trap if the bear trap snapped, would it?
def respond_to_event(type, options={})
options.reverse_merge!( :once => true,
:with => type,
:on => self.name )

handler = InvokeEventHandler.new(handler_opts)
handler = InvokeEventHandler.new(:widget_id => options[:on], :state => options[:with])

return if options[:once] and event_table.all_handlers_for(type, options[:from]).include?(handler)

Expand Down
6 changes: 6 additions & 0 deletions test/unit/event_methods_test.rb
Expand Up @@ -39,6 +39,12 @@ class EventMethodsTest < Test::Unit::TestCase
assert_equal ['answer squeak', 'answer squeak'], @mum.list
end

should "also accept an event argument only" do
@mum.respond_to_event :answer_squeak
@mum.fire :answer_squeak
assert_equal ['answer squeak'], @mum.list
end

context "#responds_to_event in class context" do
setup do
class AdultMouseCell < MouseCell
Expand Down

0 comments on commit 0f0824b

Please sign in to comment.