Class: Actor::Mailbox::Filter
- Inherits:
-
Object
- Object
- Actor::Mailbox::Filter
- Defined in:
- lib/revactor/mailbox.rb
Overview
Mailbox filterset. Takes patterns or procs to match messages with and returns the associated proc when a pattern matches.
Instance Method Summary collapse
-
#after(seconds, &action) ⇒ Object
Provide a timeout (in seconds, can be a Float) to wait for matching messages.
-
#empty? ⇒ Boolean
Is the filterset empty?.
-
#initialize(mailbox) ⇒ Filter
constructor
A new instance of Filter.
-
#match(message) ⇒ Object
Match a message using the filter.
-
#when(pattern, &action) ⇒ Object
Provide a pattern to match against with === and a block to call when the pattern is matched.
Constructor Details
#initialize(mailbox) ⇒ Filter
Returns a new instance of Filter.
113 114 115 116 |
# File 'lib/revactor/mailbox.rb', line 113 def initialize(mailbox) @mailbox = mailbox @ruleset = [] end |
Instance Method Details
#after(seconds, &action) ⇒ Object
Provide a timeout (in seconds, can be a Float) to wait for matching messages. If the timeout elapses, the given block is called.
128 129 130 131 132 133 134 135 |
# File 'lib/revactor/mailbox.rb', line 128 def after(seconds, &action) raise ArgumentError, "timeout already specified" if @mailbox.timer raise ArgumentError, "must be zero or positive" if seconds < 0 # Don't explicitly require an action to be specified @mailbox.timeout_action = action || proc {} @mailbox.timer = Timer.new(seconds, Actor.current).attach(Rev::Loop.default) end |
#empty? ⇒ Boolean
Is the filterset empty?
144 145 146 |
# File 'lib/revactor/mailbox.rb', line 144 def empty? @ruleset.empty? and not @mailbox.timer end |
#match(message) ⇒ Object
Match a message using the filter
138 139 140 141 |
# File 'lib/revactor/mailbox.rb', line 138 def match() _, action = @ruleset.find { |pattern, _| pattern === } action end |
#when(pattern, &action) ⇒ Object
Provide a pattern to match against with === and a block to call when the pattern is matched.
120 121 122 123 124 |
# File 'lib/revactor/mailbox.rb', line 120 def when(pattern, &action) # Don't explicitly require an action to be specified action ||= proc {} @ruleset << [pattern, action] end |