Class: Walnut::Events

Inherits:
Object
  • Object
show all
Defined in:
lib/walnut/events.rb

Overview

The simple yet fundamental events system of walnut.

Author:

  • noxgirl

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvents

Produce a new events system.



26
27
28
# File 'lib/walnut/events.rb', line 26

def initialize
  @events  = Hash.new
end

Instance Attribute Details

#eventsHash{String => Array<Array(Symbol, Proc)>} (readonly)

Returns The list of events.

Returns:

  • (Hash{String => Array<Array(Symbol, Proc)>})

    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

#threadsHash{String => Array<Thread>} (readonly)

Returns A list of threads.

Returns:

  • (Hash{String => Array<Thread>})

    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.

Parameters:

  • event (Symbol)

    The event for which to wait.

  • hook (Symbol)

    The identity of this hook.

Yields:

  • (...)

    The arguments which will be yielded to the block vary by event. Consult with the events specification.



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.

Parameters:

  • event (Symbol)

    The event for which this hook is waiting.

  • hook (Symbol)

    The hook.



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.

Parameters:

  • event (Symbol)

    The event which to execute.

  • args (Array<>)

    The arguments which to pass to each block. (splat)



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