Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 3ccc0bf

Browse files
committed
Callback block is execute in instance context
1 parent 19d932c commit 3ccc0bf

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

lib/react/callbacks.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def run_callback(name, *args)
1111
callbacks = self.class.send(attribute_name)
1212
callbacks.each do |callback|
1313
if callback.is_a?(Proc)
14-
callback.call(*args)
14+
instance_exec(*args, &callback)
1515
else
1616
send(callback, *args)
1717
end
@@ -27,7 +27,6 @@ def define_callback(callback_name)
2727
callbacks = self.send(attribute_name)
2828
callbacks.concat(args)
2929
callbacks.push(block) if block_given?
30-
debugger
3130
self.send("#{attribute_name}=", callbacks)
3231
end
3332
end

spec/callbacks_spec.rb

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,54 +39,69 @@ def turn_of_laptop;end
3939

4040
it "should be able to define block callback" do
4141
stub_const 'Foo', Class.new
42-
proc_a = Proc.new {}
43-
proc_b = Proc.new {}
4442
Foo.class_eval do
4543
include React::Callbacks
44+
attr_accessor :a
45+
attr_accessor :b
46+
4647
define_callback :before_dinner
4748

48-
before_dinner(&proc_a)
49-
before_dinner(&proc_b)
49+
before_dinner do
50+
self.a = 10
51+
end
52+
before_dinner do
53+
self.b = 20
54+
end
5055
end
5156

52-
expect(proc_a).to receive(:call)
53-
expect(proc_b).to receive(:call)
54-
Foo.new.run_callback(:before_dinner)
57+
foo = Foo.new
58+
foo.run_callback(:before_dinner)
59+
expect(foo.a).to eq(10)
60+
expect(foo.b).to eq(20)
5561
end
5662

5763
it "should be able to define multiple callback group" do
58-
proc_a = Proc.new {}
5964
stub_const 'Foo', Class.new
6065
Foo.class_eval do
6166
include React::Callbacks
6267
define_callback :before_dinner
6368
define_callback :after_dinner
69+
attr_accessor :a
6470

65-
before_dinner(&proc_a)
71+
before_dinner do
72+
self.a = 10
73+
end
6674
end
6775

68-
expect(proc_a).to receive(:call)
69-
Foo.new.run_callback(:before_dinner)
70-
Foo.new.run_callback(:after_dinner)
76+
foo = Foo.new
77+
foo.run_callback(:before_dinner)
78+
foo.run_callback(:after_dinner)
79+
80+
expect(foo.a).to eq(10)
7181
end
7282

7383
it "should be able to receive args as callback" do
74-
a_proc = Proc.new { }
7584
stub_const 'Foo', Class.new
7685
Foo.class_eval do
7786
include React::Callbacks
7887
define_callback :before_dinner
7988
define_callback :after_dinner
8089

81-
before_dinner(&a_proc)
90+
attr_accessor :lorem
91+
92+
before_dinner do |a, b|
93+
self.lorem = "#{a}-#{b}"
94+
end
95+
8296
after_dinner :eat_ice_cream
8397
def eat_ice_cream(a,b,c); end
8498
end
8599

86-
expect(a_proc).to receive(:call).with(1,2)
87100
expect_any_instance_of(Foo).to receive(:eat_ice_cream).with(4,5,6)
88101

89-
Foo.new.run_callback(:before_dinner, 1, 2)
90-
Foo.new.run_callback(:after_dinner, 4, 5, 6)
102+
foo = Foo.new
103+
foo.run_callback(:before_dinner, 1, 2)
104+
foo.run_callback(:after_dinner, 4, 5, 6)
105+
expect(foo.lorem).to eq('1-2')
91106
end
92107
end

0 commit comments

Comments
 (0)