Class: Walnut::Events
- Inherits:
-
Object
- Object
- Walnut::Events
- Defined in:
- lib/walnut/events.rb
Overview
The simple yet fundamental events system of walnut.
Instance Attribute Summary collapse
-
#events ⇒ Hash{String => Array<Array(Symbol, Proc)>}
readonly
The list of events.
-
#threads ⇒ Hash{String => Array<Thread>}
readonly
A list of threads.
Instance Method Summary collapse
-
#initialize ⇒ Events
constructor
Produce a new events system.
-
#nut_catch(event, hook) {|...| ... } ⇒ Object
Await an event, and upon its occurrence, execute a block of code.
-
#nut_smash(event, hook) ⇒ Object
Delete a hook from an event.
-
#nut_throw(event, *args) ⇒ Object
Execute an event, and call all the blocks of code which are hooked onto it, if any, passing to each the provided arguments.
Constructor Details
#initialize ⇒ Events
Produce a new events system.
26 27 28 |
# File 'lib/walnut/events.rb', line 26 def initialize @events = Hash.new end |
Instance Attribute Details
#events ⇒ Hash{String => Array<Array(Symbol, Proc)>} (readonly)
Returns The list of events.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/walnut/events.rb', line 21 class Events attr_reader :events, :threads # Produce a new events system. def initialize @events = Hash.new end # Await an event, and upon its occurrence, execute a block of code. It # cannot get much simpler than such. # # @param [Symbol] event The event for which to wait. # @param [Symbol] hook The identity of this hook. # # @yield [...] The arguments which will be yielded to the block vary by # event. Consult with the {file:docs/Events.md events specification}. def nut_catch(event, hook, &prc) # Create a collection in the events hash for this event if one does not # already exist. @events[event] ||= [] # Append this hook. @events[event] << [hook, prc] end # Execute an event, and call all the blocks of code which are hooked onto it, # if any, passing to each the provided arguments. # # @param [Symbol] event The event which to execute. # @param [Array<>] args The arguments which to pass to each block. (splat) def nut_throw(event, *args) # If a collection for this event exists, iterate through it. if @events.include? event @events[event].each do |hook| hook[1].call(*args) end end # if @events.include? event end # Delete a hook from an event. # # @param [Symbol] event The event for which this hook is waiting. # @param [Symbol] hook The hook. def nut_smash(event, hook) if @events.include? event @events[event].each do |h| # iterate through the hooks if h[0] == hook @events[event].delete([h[0], h[1]]) and next end end end # if @events.include? event end end |
#threads ⇒ Hash{String => Array<Thread>} (readonly)
Returns A list of threads.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/walnut/events.rb', line 21 class Events attr_reader :events, :threads # Produce a new events system. def initialize @events = Hash.new end # Await an event, and upon its occurrence, execute a block of code. It # cannot get much simpler than such. # # @param [Symbol] event The event for which to wait. # @param [Symbol] hook The identity of this hook. # # @yield [...] The arguments which will be yielded to the block vary by # event. Consult with the {file:docs/Events.md events specification}. def nut_catch(event, hook, &prc) # Create a collection in the events hash for this event if one does not # already exist. @events[event] ||= [] # Append this hook. @events[event] << [hook, prc] end # Execute an event, and call all the blocks of code which are hooked onto it, # if any, passing to each the provided arguments. # # @param [Symbol] event The event which to execute. # @param [Array<>] args The arguments which to pass to each block. (splat) def nut_throw(event, *args) # If a collection for this event exists, iterate through it. if @events.include? event @events[event].each do |hook| hook[1].call(*args) end end # if @events.include? event end # Delete a hook from an event. # # @param [Symbol] event The event for which this hook is waiting. # @param [Symbol] hook The hook. def nut_smash(event, hook) if @events.include? event @events[event].each do |h| # iterate through the hooks if h[0] == hook @events[event].delete([h[0], h[1]]) and next end end end # if @events.include? event end end |
Instance Method Details
#nut_catch(event, hook) {|...| ... } ⇒ Object
Await an event, and upon its occurrence, execute a block of code. It cannot get much simpler than such.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/walnut/events.rb', line 38 def nut_catch(event, hook, &prc) # Create a collection in the events hash for this event if one does not # already exist. @events[event] ||= [] # Append this hook. @events[event] << [hook, prc] end |
#nut_smash(event, hook) ⇒ Object
Delete a hook from an event.
71 72 73 74 75 76 77 78 79 |
# File 'lib/walnut/events.rb', line 71 def nut_smash(event, hook) if @events.include? event @events[event].each do |h| # iterate through the hooks if h[0] == hook @events[event].delete([h[0], h[1]]) and next end end end # if @events.include? event end |
#nut_throw(event, *args) ⇒ Object
Execute an event, and call all the blocks of code which are hooked onto it, if any, passing to each the provided arguments.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/walnut/events.rb', line 54 def nut_throw(event, *args) # If a collection for this event exists, iterate through it. if @events.include? event @events[event].each do |hook| hook[1].call(*args) end end # if @events.include? event end |