Skip to content

Commit db921a7

Browse files
committed
Simplify behavior definition
1 parent d16bbec commit db921a7

File tree

3 files changed

+34
-38
lines changed

3 files changed

+34
-38
lines changed

lib/concurrent/actor/behaviour.rb

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ module Behaviour
7070

7171
# Array of behaviours and their construction parameters.
7272
#
73-
# [[Behaviour::SetResults, [:terminate!]],
74-
# [Behaviour::RemovesChild, []],
75-
# [Behaviour::Termination, []],
76-
# [Behaviour::TerminatesChildren, []],
77-
# [Behaviour::Linking, []],
78-
# [Behaviour::Awaits, []],
79-
# [Behaviour::ExecutesContext, []],
80-
# [Behaviour::ErrorsOnUnknownMessage, []]]
73+
# [[Behaviour::SetResults, :terminate!],
74+
# [Behaviour::RemovesChild],
75+
# [Behaviour::Termination],
76+
# [Behaviour::TerminatesChildren],
77+
# [Behaviour::Linking],
78+
# [Behaviour::Awaits],
79+
# [Behaviour::ExecutesContext],
80+
# [Behaviour::ErrorsOnUnknownMessage]]
8181
#
8282
# @see '' its source code
8383
def self.basic_behaviour_definition
@@ -88,17 +88,16 @@ def self.basic_behaviour_definition
8888

8989
# Array of behaviours and their construction parameters.
9090
#
91-
# [[Behaviour::SetResults, [:pause!]],
92-
# [Behaviour::RemovesChild, []],
93-
# [Behaviour::Termination, []],
94-
# [Behaviour::TerminatesChildren, []],
95-
# [Behaviour::Linking, []],
96-
# [Behaviour::Supervised, []],
97-
# [Behaviour::Pausing, []],
98-
# [Behaviour::Supervising, [:reset!, :one_for_one]],
99-
# [Behaviour::Awaits, []],
100-
# [Behaviour::ExecutesContext, []],
101-
# [Behaviour::ErrorsOnUnknownMessage, []]]
91+
# [[Behaviour::SetResults, :pause!],
92+
# [Behaviour::RemovesChild],
93+
# [Behaviour::Termination],
94+
# [Behaviour::TerminatesChildren],
95+
# [Behaviour::Linking],
96+
# [Behaviour::Pausing],
97+
# [Behaviour::Supervising, :reset!, :one_for_one],
98+
# [Behaviour::Awaits],
99+
# [Behaviour::ExecutesContext],
100+
# [Behaviour::ErrorsOnUnknownMessage]]
102101
#
103102
# @see '' its source code
104103
def self.restarting_behaviour_definition(handle = :reset!, strategy = :one_for_one)
@@ -111,33 +110,33 @@ def self.restarting_behaviour_definition(handle = :reset!, strategy = :one_for_o
111110

112111
# @see '' its source code
113112
def self.base(on_error)
114-
[[SetResults, [on_error]],
113+
[[SetResults, on_error],
115114
# has to be before Termination to be able to remove children form terminated actor
116-
[RemovesChild, []],
117-
[Termination, []],
118-
[TerminatesChildren, []]]
115+
RemovesChild,
116+
Termination,
117+
TerminatesChildren]
119118
end
120119

121120
# @see '' its source code
122121
def self.linking
123-
[[Linking, []]]
122+
[Linking]
124123
end
125124

126125
# @see '' its source code
127126
def self.supervised
128-
[[Pausing, []]]
127+
[Pausing]
129128
end
130129

131130
# @see '' its source code
132131
def self.supervising(handle = :reset!, strategy = :one_for_one)
133-
[[Behaviour::Supervising, [handle, strategy]]]
132+
[[Behaviour::Supervising, handle, strategy]]
134133
end
135134

136135
# @see '' its source code
137136
def self.user_messages
138-
[[Awaits, []],
139-
[ExecutesContext, []],
140-
[ErrorsOnUnknownMessage, []]]
137+
[Awaits,
138+
ExecutesContext,
139+
ErrorsOnUnknownMessage]
141140
end
142141
end
143142
end

lib/concurrent/actor/core.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,12 @@ def process_envelope(envelope)
202202
private
203203

204204
def initialize_behaviours(opts)
205-
@behaviour_definition = (Type! opts[:behaviour_definition] || @context.behaviour_definition, Array).each do |v|
206-
Type! v, Array
207-
Match! v.size, 2
208-
Child! v[0], Behaviour::Abstract
209-
Type! v[1], Array
205+
@behaviour_definition = (Type! opts[:behaviour_definition] || @context.behaviour_definition, Array).each do |(behaviour, *args)|
206+
Child! behaviour, Behaviour::Abstract
210207
end
211208
@behaviours = {}
212209
@first_behaviour = @behaviour_definition.reverse.
213-
reduce(nil) { |last, (behaviour, args)| @behaviours[behaviour] = behaviour.new(self, last, opts, *args) }
210+
reduce(nil) { |last, (behaviour, *args)| @behaviours[behaviour] = behaviour.new(self, last, opts, *args) }
214211
end
215212
end
216213
end

spec/concurrent/actor_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,11 @@ def on_message(message)
338338

339339
it 'pauses on error and restarts' do
340340
queue = Queue.new
341-
resuming_behaviour = Behaviour.restarting_behaviour_definition.map do |c, args|
341+
resuming_behaviour = Behaviour.restarting_behaviour_definition.map do |c, *args|
342342
if Behaviour::Supervising == c
343-
[c, [:restart!, :one_for_one]]
343+
[c, *[:restart!, :one_for_one]]
344344
else
345-
[c, args]
345+
[c, *args]
346346
end
347347
end
348348

0 commit comments

Comments
 (0)