Class: Actor::Mailbox::Filter

Inherits:
Object
  • Object
show all
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

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.

Raises:

  • (ArgumentError)


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?

Returns:

  • (Boolean)


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(message)
  _, action = @ruleset.find { |pattern, _| pattern === message }
  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