File tree Expand file tree Collapse file tree 3 files changed +37
-10
lines changed Expand file tree Collapse file tree 3 files changed +37
-10
lines changed Original file line number Diff line number Diff line change @@ -15,12 +15,21 @@ def initialize
15
15
# @param [Object] observer the observer to add
16
16
# @param [Symbol] func the function to call on the observer during notification. Default is :update
17
17
# @return [Symbol] the added function
18
- def add_observer ( observer , func = :update )
18
+ def add_observer ( observer = nil , func = :update , &block )
19
+ unless !!observer ^ block # xor
20
+ raise ArgumentError , 'should pass observer as a first argument or block'
21
+ end
22
+
23
+ if block
24
+ observer = block
25
+ func = :call
26
+ end
27
+
19
28
@mutex . lock
20
29
@observers [ observer ] = func
21
30
@mutex . unlock
22
31
23
- func
32
+ observer
24
33
end
25
34
26
35
# @param [Object] observer the observer to remove
Original file line number Diff line number Diff line change @@ -14,14 +14,23 @@ def initialize
14
14
# @param [Object] observer the observer to add
15
15
# @param [Symbol] func the function to call on the observer during notification. Default is :update
16
16
# @return [Symbol] the added function
17
- def add_observer ( observer , func = :update )
17
+ def add_observer ( observer = nil , func = :update , &block )
18
+ unless !!observer ^ block # xor
19
+ raise ArgumentError , 'should pass observer as a first argument or block'
20
+ end
21
+
22
+ if block
23
+ observer = block
24
+ func = :call
25
+ end
26
+
18
27
@mutex . lock
19
28
new_observers = @observers . dup
20
29
new_observers [ observer ] = func
21
30
@observers = new_observers
22
31
@mutex . unlock
23
32
24
- func
33
+ observer
25
34
end
26
35
27
36
# @param [Object] observer the observer to remove
Original file line number Diff line number Diff line change 8
8
9
9
describe '#add_observer' do
10
10
11
- context 'with argument ' do
12
- it 'should return the passed function ' do
13
- observer_set . add_observer ( observer , :a_method ) . should eq ( :a_method )
11
+ context 'with arguments ' do
12
+ it 'should return the observer ' do
13
+ observer_set . add_observer ( observer , :a_method ) . should == observer
14
14
end
15
15
end
16
16
17
- context 'without arguments' do
18
- it 'should return the default function' do
19
- observer_set . add_observer ( observer ) . should eq ( :update )
17
+ context 'with a block' do
18
+ it 'should return the observer based on a block' do
19
+ observer = observer_set . add_observer { :block }
20
+ observer . call . should == :block
20
21
end
21
22
end
22
23
end
61
62
observer_set . notify_observers ( 'a string arg' )
62
63
end
63
64
65
+ it 'should notify an observer from a block' do
66
+ notification = double
67
+ expect ( notification ) . to receive ( :catch )
68
+
69
+ observer_set . add_observer { |arg | arg . catch }
70
+ observer_set . notify_observers notification
71
+ end
72
+
64
73
it 'can be called many times' do
65
74
expect ( observer ) . to receive ( :update ) . with ( :an_arg ) . twice
66
75
expect ( observer ) . to receive ( :update ) . with ( no_args ) . once
You can’t perform that action at this time.
0 commit comments