@@ -12,20 +12,20 @@ def item
12
12
end
13
13
14
14
class SumAll
15
- ALL = [ ]
16
15
attr_reader :own_value
17
16
attr_accessor :value
18
17
19
- def initialize ( own_value )
18
+ def initialize ( ctx , own_value )
20
19
@own_value = own_value
21
- ALL << self
20
+ @all = ctx [ :__sum_all__ ] ||= [ ]
21
+ @all << self
22
22
end
23
23
24
24
def value
25
25
@value ||= begin
26
- total_value = ALL . map ( &:own_value ) . reduce ( &:+ )
27
- ALL . each { |v | v . value = total_value }
28
- ALL . clear
26
+ total_value = @all . map ( &:own_value ) . reduce ( &:+ )
27
+ @all . each { |v | v . value = total_value }
28
+ @all . clear
29
29
total_value
30
30
end
31
31
@value
@@ -39,7 +39,7 @@ def value
39
39
end
40
40
field :nestedSum , BoxedSum do
41
41
argument :value , !types . Int
42
- resolve -> ( o , args , c ) { SumAll . new ( o + args [ :value ] ) }
42
+ resolve -> ( o , args , c ) { SumAll . new ( c , o + args [ :value ] ) }
43
43
end
44
44
end
45
45
@@ -54,12 +54,12 @@ def value
54
54
55
55
field :sum , !types . Int do
56
56
argument :value , !types . Int
57
- resolve -> ( o , args , c ) { SumAll . new ( args [ :value ] ) }
57
+ resolve -> ( o , args , c ) { SumAll . new ( c , args [ :value ] ) }
58
58
end
59
59
60
60
field :nestedSum , BoxedSum do
61
61
argument :value , !types . Int
62
- resolve -> ( o , args , c ) { SumAll . new ( args [ :value ] ) }
62
+ resolve -> ( o , args , c ) { SumAll . new ( c , args [ :value ] ) }
63
63
end
64
64
65
65
field :listSum , types [ BoxedSum ] do
@@ -155,7 +155,7 @@ class SubBox < Box; end
155
155
map . set ( SumAll , :value )
156
156
b = Box . new ( 1 )
157
157
sub_b = SubBox . new ( 2 )
158
- s = SumAll . new ( 3 )
158
+ s = SumAll . new ( { } , 3 )
159
159
assert_equal ( :item , map . get ( b ) )
160
160
assert_equal ( :item , map . get ( sub_b ) )
161
161
assert_equal ( :value , map . get ( s ) )
0 commit comments