1
+ # This is test code.
2
+ #
3
+ # Avoid using organizers predominantly composed of lambdas. In complex organizers,
4
+ # it's common to have many isolated interactor classes, but maintain some orchestration
5
+ # code for readability and cohesion.
6
+ #
7
+ # For instance, rather than creating an EachProduct interactor or a separate interactor chain
8
+ # for a single boolean flag, it's more efficient to manage the high-level business process
9
+ # in one location. This approach reduces the need to switch between two mindsets:
10
+ #
11
+ # Mode 1 (High-Level Overview): Understanding the business process and identifying where changes fit.
12
+ # Mode 2 (Detailed Implementation): Focusing on the specific logic of a process step.
13
+ #
14
+ # Initially, in Mode 1, a bird's-eye view is essential for grasping the process architecture.
15
+ # Once this overview is clear, you pinpoint the relevant interactor for modifications or additions,
16
+ # transitioning to Mode 2 for implementation.
17
+ #
18
+ # This distinction between modes is crucial. Mode 1 involves more reading, conceptualizing,
19
+ # and questioning, often requiring navigation through multiple files and note-taking.
20
+ # Mode 2 is about active coding and editing. Minimizing context switching between these modes
21
+ # saves time and mental energy. It's also easier to maintain a high-level overview of the process
22
+ # when the code is in one place.
23
+ #
24
+ # Typically this will allow us to move closer towards a one organizer per interaction model.
25
+ # E.g. one Rails action == one organizer.
26
+ # Even the background job firing can be handled by the organizer via YourClass::Async.
27
+ #
28
+ # An improvement we aim for is automatic diagram generation from the organizers, utilizing
29
+ # contracts for documentation.
30
+ #
31
+ # Note: The conditional structures (`if`, `each`) here are not executed at runtime in the usual
32
+ # sense. They are evaluated when defining the organizer, leading to the creation of discrete
33
+ # classes that handle the respective logic.
34
+ #
1
35
class AllTheThings
2
36
include Interactify
3
37
@@ -12,34 +46,44 @@ class AllTheThings
12
46
else : [ If ::C , If ::D ]
13
47
) ,
14
48
15
- self . each (
16
- :things ,
17
- If ::A ,
18
- If ::B ,
19
- -> ( c ) { c . lambda_set = true }
49
+ # test each with lambda
50
+ self . if (
51
+ -> ( c ) { c . things } ,
52
+ self . each (
53
+ :things ,
54
+ If ::A ,
55
+ If ::B ,
56
+ -> ( c ) { c . lambda_set = true }
57
+ ) ,
20
58
) ,
21
59
60
+ # test alternative if syntax
22
61
self . if (
23
62
-> ( c ) { c . a && c . b } ,
24
63
then : -> ( c ) { c . both_a_and_b = true } ,
25
64
else : -> ( c ) { c . both_a_and_b = false }
26
65
) ,
27
66
67
+ # test setting a value to use later in the chain
28
68
-> ( c ) { c . more_things = [ 1 , 2 , 3 , 4 ] } ,
29
69
70
+ # test lambdas inside each
30
71
self . each (
31
72
:more_things ,
32
73
-> ( c ) {
33
74
if c . more_thing_index . zero?
34
75
c . first_more_thing = true
35
76
end
36
77
} ,
37
- -> ( c ) {
78
+ -> ( c ) {
38
79
if c . more_thing_index == 1
39
80
c . next_more_thing = true
40
81
end
41
82
} ,
83
+ # test nested if inside each
42
84
{ if : :not_set_thing , then : -> ( c ) { c . not_set_thing = true } } ,
85
+
86
+ # test setting a value after an else
43
87
-> ( c ) { c . more_thing = true }
44
88
) ,
45
89
@@ -49,11 +93,47 @@ class AllTheThings
49
93
-> ( c ) { c . optional_thing_was_set = true } ,
50
94
-> ( c ) { c . and_then_another_thing = true } ,
51
95
-> ( c ) { c . and_one_more_thing = true } ,
96
+ # test nested each inside if
52
97
self . each ( :more_things ,
53
98
-> ( c ) { c . more_things [ c . more_thing_index ] = c . more_thing + 5 } ,
54
99
-> ( c ) { c . more_things [ c . more_thing_index ] = c . more_thing + 5 }
55
- )
100
+ )
56
101
] ,
57
102
else : -> ( c ) { c . optional_thing_was_set = false }
103
+ ) ,
104
+
105
+ # if -> each -> if -> each
106
+ self . if ( :condition , then : [ -> { } ] , else : [
107
+ self . each (
108
+ :things ,
109
+ self . if (
110
+ :thing ,
111
+ then : self . each (
112
+ :more_things ,
113
+ -> ( c ) {
114
+ c . counter ||= 0
115
+ c . counter += 1
116
+ }
117
+ )
118
+ )
119
+ )
120
+ ] ) ,
121
+
122
+ # each -> each -> each
123
+ self . each (
124
+ :more_things ,
125
+ self . each (
126
+ :more_things ,
127
+ self . each (
128
+ :more_things ,
129
+ self . each (
130
+ :more_things ,
131
+ -> ( c ) {
132
+ c . heavily_nested_counter ||= 0
133
+ c . heavily_nested_counter += 1
134
+ }
135
+ )
136
+ )
137
+ )
58
138
)
59
139
end
0 commit comments