steppy 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 987fb5fa5cd7883b0604fb5c0d29ca5bb955d4d34f9d53e21406ab78fbb5f40c
4
- data.tar.gz: c9f3778df2fd3edaf6a2b3daa12cf69deb35f876eecc8911ceebe8c488192452
3
+ metadata.gz: b004c99544ed8e124ccdf1e699a421a0a70c1515ef2c095f26c2d50b0f76b97a
4
+ data.tar.gz: 40ff04003443747d336ff6cab8771849ceff5f97282160e4581674d75ea0baf6
5
5
  SHA512:
6
- metadata.gz: daa02f43a1ad9615b1a1508c0b7fad026786131d2d4a6dd22b572dbea4b5e6ee1c8cce0ede8ea06a805a25d56c96e408c9d82d45d5e9be53bb4323e671274e37
7
- data.tar.gz: 1cc10dee46a0c168e8a42923cc3f5322bc892ce62ac0f443b4231ce843a390f611f6ab2eda47d608db781f0d3222ba84ff4d1a6f69ad0c6458d5d1d1760bd2ce
6
+ metadata.gz: 5d8cda58c821abafe498dedb044e849c5cfb4e25a2e4d81e9113dee0c2bdb9927533421ac2046e32af84bc0f4092559d9b3996d6254f76f7afca78162fc71a9b
7
+ data.tar.gz: 42c3c4587b6aa2fba5848a0e67617000603f0729eaefd11826b45d138532f5bab604649518f0b078b9f80be8c5c206910353c790250f2ee7a6b49aafb3d854e0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- steppy (1.0.1)
4
+ steppy (1.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/steppy.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require 'steppy/version'
4
4
  require 'steppy/cache'
5
5
  require 'steppy/error'
6
+ require 'steppy/class_methods'
7
+ require 'steppy/instance_methods'
6
8
 
7
9
  # The Steppy module you'll include in your classes to give them steps!
8
10
  module Steppy
@@ -26,267 +28,4 @@ module Steppy
26
28
 
27
29
  { method: method, args: args, block: block }
28
30
  end
29
-
30
- # Steppy class methods that will be added to your included classes.
31
- module ClassMethods
32
- def steppy(&block)
33
- steppy_cache[:block] = block
34
- end
35
-
36
- def step_set(*sets)
37
- steppy_cache[:sets] += sets
38
- end
39
-
40
- def step(method = nil, args = {}, &block)
41
- steps.push(
42
- Steppy.parse_step(method: method, args: args, block: block)
43
- )
44
- self
45
- end
46
-
47
- alias step_return step
48
-
49
- def step_if(condition, &block)
50
- steps.push(condition: condition, block: block)
51
- self
52
- end
53
-
54
- def step_unless(condition, &block)
55
- steps.push(condition: -> { !steppy_run_condition(condition) }, block: block)
56
- self
57
- end
58
-
59
- def step_rescue(exceptions = nil, &block)
60
- steppy_cache[:rescues].push(exceptions: exceptions, block: block)
61
- self
62
- end
63
-
64
- def step_if_else(condition_block, step_steps, args = {})
65
- if_step, else_step = step_steps
66
-
67
- steps.push Steppy.parse_step(
68
- method: if_step,
69
- args: {
70
- if: condition_block,
71
- }.merge(args)
72
- )
73
-
74
- steps.push Steppy.parse_step(
75
- method: else_step,
76
- args: {
77
- unless: condition_block,
78
- }.merge(args)
79
- )
80
- end
81
-
82
- def step_after(key = nil, &block)
83
- step_add_callback(:after, block, key)
84
- end
85
-
86
- def step_before(key = nil, &block)
87
- step_add_callback(:before, block, key)
88
- end
89
-
90
- def step_add_callback(type, block, key)
91
- callback_key = key ? key.to_sym : :global
92
- callbacks = step_callbacks[type][callback_key] ||= []
93
- callbacks.push(block)
94
- end
95
-
96
- def steps
97
- steppy_cache[:steps]
98
- end
99
-
100
- def step_callbacks
101
- steppy_cache[:callbacks]
102
- end
103
-
104
- def steppy_cache
105
- @steppy_cache ||= SteppyCache.new(
106
- steps: [],
107
- sets: [],
108
- rescues: [],
109
- callbacks: {
110
- before: {
111
- global: [],
112
- },
113
- after: {
114
- global: [],
115
- },
116
- }
117
- )
118
- end
119
- end
120
-
121
- # Steppy instance methods that will be added.
122
- module InstanceMethods
123
- attr_reader :steppy_cache
124
-
125
- def steppy(attributes = {}, cache = {})
126
- steppy_initialize_cache({ attributes: attributes, prefix: :step }.merge(cache))
127
-
128
- if steppy_cache.key?(:block)
129
- instance_exec(&steppy_cache[:block])
130
- else
131
- steppy_run(steppy_cache)
132
- end
133
- rescue StandardError => exception
134
- steppy_rescue exception, steppy_cache[:rescues]
135
- end
136
-
137
- def step_set(*sets)
138
- steppy_sets(sets)
139
- end
140
-
141
- def step(method = nil, args = {}, &block)
142
- steppy_run_step Steppy.parse_step(method: method, args: args, block: block)
143
- end
144
-
145
- alias step_return step
146
-
147
- def step_if_else(condition_block, step_steps, args = {})
148
- if_step, else_step = step_steps
149
-
150
- steppy_run_step Steppy.parse_step(
151
- method: if_step,
152
- args: {
153
- if: condition_block,
154
- }.merge(args)
155
- )
156
-
157
- steppy_run_step Steppy.parse_step(
158
- method: else_step,
159
- args: {
160
- unless: condition_block,
161
- }.merge(args)
162
- )
163
- end
164
-
165
- def step_if(condition, &block)
166
- steppy_run_condition_block condition, block
167
- end
168
-
169
- def step_unless(condition, &block)
170
- steppy_run_condition_block -> { !steppy_run_condition(condition) }, block
171
- end
172
-
173
- def step_rescue(*)
174
- raise '#step_rescue can not be used in a block, please just add rescue to the #steppy block.'
175
- end
176
-
177
- protected
178
-
179
- def steppy_run(steps:, sets:, **)
180
- steppy_sets(sets)
181
- steppy_steps(steps)
182
- end
183
-
184
- def steppy_sets(sets)
185
- sets.each { |key, value| steppy_set(key, value || steppy_attributes[key]) }
186
- end
187
-
188
- def steppy_set(key, value)
189
- key && instance_variable_set("@#{key}", value)
190
- end
191
-
192
- def steppy_steps(steps)
193
- steps.each do |step|
194
- condition = step[:condition]
195
-
196
- steppy_cache[:result] = if condition
197
- steppy_run_condition_block(condition, step[:block])
198
- else
199
- steppy_run_step(step)
200
- end
201
- end
202
-
203
- steppy_result
204
- end
205
-
206
- def steppy_rescue(exception, rescues)
207
- exception_class = exception.class
208
- has_exception = exception_class == SteppyError || rescues.empty?
209
-
210
- raise exception if has_exception
211
-
212
- rescues.each do |exceptions:, block:|
213
- if !exceptions || (exceptions && !exceptions.include?(exception_class))
214
- steppy_cache[:result] = instance_exec(steppy_attributes, &block)
215
- end
216
- end
217
-
218
- steppy_result
219
- end
220
-
221
- def steppy_run_condition_block(condition, block)
222
- steppy_run(steppy_cache_from_block(block)) if steppy_run_condition(condition)
223
- end
224
-
225
- def steppy_run_condition(condition)
226
- return true unless condition
227
-
228
- if condition.arity > 0
229
- instance_exec(steppy_attributes, &condition)
230
- else
231
- instance_exec(&condition)
232
- end
233
- end
234
-
235
- def steppy_run_step(method:, args:, block:)
236
- if !steppy_run_condition(args[:condition]) || (steppy_cache[:prefix] != args[:prefix])
237
- return steppy_result
238
- end
239
-
240
- step_callbacks(:before, method, args, args)
241
-
242
- result = if block
243
- instance_exec(steppy_attributes, &block)
244
- else
245
- steppy_run_method(method, steppy_attributes)
246
- end
247
-
248
- step_callbacks(:after, method, result, args)
249
-
250
- steppy_set(args[:set], result)
251
-
252
- result
253
- end
254
-
255
- def step_callbacks(type, method, result, args)
256
- callbacks = steppy_cache[:callbacks][type]
257
- method_callbacks = (callbacks[method] || [])
258
-
259
- callbacks[:global].concat(method_callbacks).each do |callback|
260
- instance_exec(result, args, &callback)
261
- end
262
- end
263
-
264
- def steppy_run_method(method_name, attributes)
265
- method = "#{steppy_cache[:prefix]}_#{method_name}"
266
-
267
- if method(method).arity > 0
268
- public_send(method, attributes)
269
- else
270
- public_send(method)
271
- end
272
- end
273
-
274
- def steppy_cache_from_block(block)
275
- Class.new { include Steppy }.instance_exec(&block).steppy_cache
276
- end
277
-
278
- def steppy_initialize_cache(cache)
279
- @steppy_cache = SteppyCache.new(
280
- self.class.steppy_cache.to_h.merge(result: nil).merge(cache)
281
- )
282
- end
283
-
284
- def steppy_attributes
285
- steppy_cache[:attributes]
286
- end
287
-
288
- def steppy_result
289
- steppy_cache[:result]
290
- end
291
- end
292
31
  end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Steppy
4
+ # Steppy class methods that will be added to your included classes.
5
+ module ClassMethods
6
+ def steppy(&block)
7
+ steppy_cache[:block] = block
8
+ end
9
+
10
+ def step_set(*sets)
11
+ steppy_cache[:sets] += sets
12
+ end
13
+
14
+ def step(method = nil, args = {}, &block)
15
+ steps.push(
16
+ Steppy.parse_step(method: method, args: args, block: block)
17
+ )
18
+ self
19
+ end
20
+
21
+ alias step_return step
22
+
23
+ def step_if(condition, &block)
24
+ steps.push(condition: condition, block: block)
25
+ self
26
+ end
27
+
28
+ def step_unless(condition, &block)
29
+ steps.push(condition: -> { !steppy_run_condition(condition) }, block: block)
30
+ self
31
+ end
32
+
33
+ def step_rescue(exceptions = nil, &block)
34
+ steppy_cache[:rescues].push(exceptions: exceptions, block: block)
35
+ self
36
+ end
37
+
38
+ def step_if_else(condition_block, step_steps, args = {})
39
+ if_step, else_step = step_steps
40
+
41
+ steps.push Steppy.parse_step(
42
+ method: if_step,
43
+ args: {
44
+ if: condition_block,
45
+ }.merge(args)
46
+ )
47
+
48
+ steps.push Steppy.parse_step(
49
+ method: else_step,
50
+ args: {
51
+ unless: condition_block,
52
+ }.merge(args)
53
+ )
54
+ end
55
+
56
+ def step_after(key = nil, &block)
57
+ step_add_callback(:after, block, key)
58
+ end
59
+
60
+ def step_before(key = nil, &block)
61
+ step_add_callback(:before, block, key)
62
+ end
63
+
64
+ def step_add_callback(type, block, key)
65
+ callback_key = key ? key.to_sym : :global
66
+ callbacks = step_callbacks[type][callback_key] ||= []
67
+ callbacks.push(block)
68
+ end
69
+
70
+ def steps
71
+ steppy_cache[:steps]
72
+ end
73
+
74
+ def step_callbacks
75
+ steppy_cache[:callbacks]
76
+ end
77
+
78
+ def steppy_cache
79
+ @steppy_cache ||= SteppyCache.new(
80
+ steps: [],
81
+ sets: [],
82
+ rescues: [],
83
+ callbacks: {
84
+ before: {
85
+ global: [],
86
+ },
87
+ after: {
88
+ global: [],
89
+ },
90
+ }
91
+ )
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,183 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Steppy
4
+ # Steppy instance methods that will be added.
5
+ module InstanceMethods
6
+ attr_reader :steppy_cache
7
+
8
+ def steppy(attributes = {}, cache = {})
9
+ steppy_initialize_cache({ attributes: attributes, prefix: :step }.merge(cache))
10
+
11
+ if steppy_cache.key?(:block)
12
+ instance_exec(&steppy_cache[:block])
13
+ else
14
+ steppy_run(steppy_cache)
15
+ end
16
+ rescue StandardError => exception
17
+ steppy_rescue exception, steppy_cache[:rescues]
18
+ end
19
+
20
+ def step_set(*sets)
21
+ steppy_sets(sets)
22
+ end
23
+
24
+ def step(method = nil, args = {}, &block)
25
+ steppy_run_step Steppy.parse_step(method: method, args: args, block: block)
26
+ end
27
+
28
+ alias step_return step
29
+
30
+ def step_if_else(condition_block, step_steps, args = {})
31
+ if_step, else_step = step_steps
32
+
33
+ steppy_run_step Steppy.parse_step(
34
+ method: if_step,
35
+ args: {
36
+ if: condition_block,
37
+ }.merge(args)
38
+ )
39
+
40
+ steppy_run_step Steppy.parse_step(
41
+ method: else_step,
42
+ args: {
43
+ unless: condition_block,
44
+ }.merge(args)
45
+ )
46
+ end
47
+
48
+ def step_if(condition, &block)
49
+ steppy_run_condition_block condition, block
50
+ end
51
+
52
+ def step_unless(condition, &block)
53
+ steppy_run_condition_block -> { !steppy_run_condition(condition) }, block
54
+ end
55
+
56
+ def step_rescue(*)
57
+ raise '#step_rescue can not be used in a block, please just add rescue to the #steppy block.'
58
+ end
59
+
60
+ protected
61
+
62
+ def steppy_run(steps:, sets:, **)
63
+ steppy_sets(sets)
64
+ steppy_steps(steps)
65
+ end
66
+
67
+ def steppy_sets(sets)
68
+ sets.each { |key, value| steppy_set(key, value || steppy_attributes[key]) }
69
+ end
70
+
71
+ def steppy_set(key, value)
72
+ key && instance_variable_set("@#{key}", value)
73
+ end
74
+
75
+ def steppy_steps(steps)
76
+ steps.each do |step|
77
+ condition = step[:condition]
78
+
79
+ steppy_cache[:result] = if condition
80
+ steppy_run_condition_block(condition, step[:block])
81
+ else
82
+ steppy_run_step(step)
83
+ end
84
+ end
85
+
86
+ steppy_result
87
+ end
88
+
89
+ def steppy_rescue(exception, rescues)
90
+ exception_class = exception.class
91
+ has_exception = exception_class == SteppyError || rescues.empty?
92
+
93
+ raise exception if has_exception
94
+
95
+ rescues.each do |exceptions:, block:|
96
+ if !exceptions || (exceptions && !exceptions.include?(exception_class))
97
+ steppy_cache[:result] = instance_exec(steppy_attributes, &block)
98
+ end
99
+ end
100
+
101
+ steppy_result
102
+ end
103
+
104
+ def steppy_run_condition_block(condition, block)
105
+ steppy_run(steppy_cache_from_block(block)) if steppy_run_condition(condition)
106
+ end
107
+
108
+ def steppy_run_condition(condition)
109
+ return true unless condition
110
+
111
+ if condition.arity > 0
112
+ instance_exec(steppy_attributes, &condition)
113
+ else
114
+ instance_exec(&condition)
115
+ end
116
+ end
117
+
118
+ def steppy_run_step(method:, args:, block:)
119
+ if !steppy_run_condition(args[:condition]) || (steppy_cache[:prefix] != args[:prefix])
120
+ return steppy_result
121
+ end
122
+
123
+ step_run_callbacks(:before, method, args, args)
124
+
125
+ result = if block
126
+ instance_exec(steppy_attributes, &block)
127
+ else
128
+ steppy_run_method(method, steppy_attributes)
129
+ end
130
+
131
+ step_run_callbacks(:after, method, result, args)
132
+
133
+ steppy_set(args[:set], result)
134
+
135
+ result
136
+ end
137
+
138
+ def step_run_callbacks(type, method, result, args)
139
+ callbacks = step_callbacks[type]
140
+ method_callbacks = callbacks[method] || []
141
+
142
+ callbacks[:global].each do |callback|
143
+ instance_exec(result, args, &callback)
144
+ end
145
+
146
+ method_callbacks.each do |callback|
147
+ instance_exec(result, args, &callback)
148
+ end
149
+ end
150
+
151
+ def steppy_run_method(method_name, attributes)
152
+ method = "#{steppy_cache[:prefix]}_#{method_name}"
153
+
154
+ if method(method).arity > 0
155
+ public_send(method, attributes)
156
+ else
157
+ public_send(method)
158
+ end
159
+ end
160
+
161
+ def step_callbacks
162
+ steppy_cache[:callbacks]
163
+ end
164
+
165
+ def steppy_cache_from_block(block)
166
+ Class.new { include Steppy }.instance_exec(&block).steppy_cache
167
+ end
168
+
169
+ def steppy_initialize_cache(cache)
170
+ @steppy_cache = SteppyCache.new(
171
+ self.class.steppy_cache.to_h.merge(result: nil).merge(cache)
172
+ )
173
+ end
174
+
175
+ def steppy_attributes
176
+ steppy_cache[:attributes]
177
+ end
178
+
179
+ def steppy_result
180
+ steppy_cache[:result]
181
+ end
182
+ end
183
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Steppy
4
- VERSION = '1.0.1'
4
+ VERSION = '1.0.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steppy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
@@ -158,7 +158,9 @@ files:
158
158
  - hooks/pre-commit
159
159
  - lib/steppy.rb
160
160
  - lib/steppy/cache.rb
161
+ - lib/steppy/class_methods.rb
161
162
  - lib/steppy/error.rb
163
+ - lib/steppy/instance_methods.rb
162
164
  - lib/steppy/version.rb
163
165
  - steppy.gemspec
164
166
  homepage: https://github.com/cj/steppy