Module: SKUI::Events
- Included in:
- Base
- Defined in:
- src/SKUI/events.rb
Overview
Defined Under Namespace
Modules: EventDefinitions
Class Method Summary collapse
-
.included(including_module) ⇒ Object
In order to create an class instance variable for each control type that holds a list of the valid events for the control this .
Instance Method Summary collapse
-
#add_event_handler(event, &block) ⇒ nil
(also: #on)
Adds an event handler to the stack.
- #initialize ⇒ Object
-
#release_events ⇒ nil
Detaches all event handlers.
-
#trigger_event(event, *args) ⇒ Boolean
Triggers the given event.
Class Method Details
.included(including_module) ⇒ Object
In order to create an class instance variable for each control type that holds a list of the valid events for the control this
Adapted from: www.railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/ www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/
19 20 21 22 23 24 25 |
# File 'src/SKUI/events.rb', line 19 def self.included( including_module ) # Hash table of valid events for the class. # > Key: (Symbol) # > Value: (Symbol) including_module.instance_variable_set( :@control_events, {} ) including_module.extend( EventDefinitions ) end |
Instance Method Details
#add_event_handler(event, &block) ⇒ nil Also known as: on
Adds an event handler to the stack. Each event can have multiple handlers.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'src/SKUI/events.rb', line 42 def add_event_handler( event, &block ) unless self.class.has_event?( event ) raise( ArgumentError, "Event #{event} not defined for #{self.class}" ) end unless block_given? raise( ArgumentError, 'No block given.' ) end @events[event] ||= [] @events[event] << block nil end |
#initialize ⇒ Object
28 29 30 31 32 33 |
# File 'src/SKUI/events.rb', line 28 def initialize super() # Hash with Symbols for keys idenitfying the event. # Each event is an array of Proc's. @events = {} end |
#release_events ⇒ nil
Detaches all event handlers. Useful when one want to allow the objects to be garbage collected.
60 61 62 63 |
# File 'src/SKUI/events.rb', line 60 def release_events @events.clear nil end |
#trigger_event(event, *args) ⇒ Boolean
Triggers the given event. All attached procs for the given event will be called. If any event handler raises an error the remaining handlers will not be executed.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'src/SKUI/events.rb', line 74 def trigger_event( event, *args ) #Debug.puts( "#{self.class}.trigger_event(#{event.to_s})" ) #Debug.puts( args.inspect ) if @events.key?( event ) @events[event].each { |proc| # Add self to argument list so the called event can get the handle for # the control triggering it. if args.empty? proc.call( self ) else args.unshift( self ) proc.call( *args ) end } true else false end end |