steppy 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/Gemfile.lock +6 -6
- data/lib/steppy.rb +130 -108
- data/lib/steppy/cache.rb +4 -0
- data/lib/steppy/error.rb +5 -5
- data/lib/steppy/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a9fa807ba5d30abc006adf68842988e5fb4fb48a77407fc3b34ba92494a375ba
|
4
|
+
data.tar.gz: 79da4b077d8c45dcd60486831bd29242446533cb7d7f3a9b32d351ef47683d88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d080019e5966955f4278671c4ec3400268048781c4c3b7c2e2b67fea2fbe3e8c953583f0848cb3c7a2f7eb086b8d99c72d6f3042e0c7cadaafb6e55743796db3
|
7
|
+
data.tar.gz: b3466168ec557ce684a226b1bc516f54736dc2d9f80d062ee599199e943b637dfa2921f20b849a52d2265edc3ee8578f7279b9e5cf8438816d7479a17d655cf8
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
steppy (0.
|
4
|
+
steppy (0.7.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -24,12 +24,12 @@ GEM
|
|
24
24
|
json (2.1.0)
|
25
25
|
minitest (5.11.3)
|
26
26
|
parallel (1.12.1)
|
27
|
-
parser (2.5.1.
|
27
|
+
parser (2.5.1.2)
|
28
28
|
ast (~> 2.4.0)
|
29
|
-
powerpack (0.1.
|
29
|
+
powerpack (0.1.2)
|
30
30
|
rainbow (3.0.0)
|
31
31
|
rake (10.5.0)
|
32
|
-
reek (4.8.
|
32
|
+
reek (4.8.2)
|
33
33
|
codeclimate-engine-rb (~> 0.4.0)
|
34
34
|
parser (>= 2.5.0.0, < 2.6)
|
35
35
|
rainbow (>= 2.0, < 4.0)
|
@@ -45,14 +45,14 @@ GEM
|
|
45
45
|
rubocop-rspec (= 1.22.1)
|
46
46
|
rubocop-rspec (1.22.1)
|
47
47
|
rubocop (>= 0.52.1)
|
48
|
-
ruby-progressbar (1.
|
48
|
+
ruby-progressbar (1.10.0)
|
49
49
|
simplecov (0.16.1)
|
50
50
|
docile (~> 1.1)
|
51
51
|
json (>= 1.8, < 3)
|
52
52
|
simplecov-html (~> 0.10.0)
|
53
53
|
simplecov-html (0.10.2)
|
54
54
|
thread_safe (0.3.6)
|
55
|
-
unicode-display_width (1.
|
55
|
+
unicode-display_width (1.4.0)
|
56
56
|
virtus (1.0.5)
|
57
57
|
axiom-types (~> 0.1)
|
58
58
|
coercible (~> 1.0)
|
data/lib/steppy.rb
CHANGED
@@ -11,178 +11,200 @@ module Steppy
|
|
11
11
|
base.include InstanceMethods
|
12
12
|
end
|
13
13
|
|
14
|
-
#
|
15
|
-
|
16
|
-
if
|
17
|
-
|
18
|
-
args[:prefix] ||= :step
|
19
|
-
|
20
|
-
steppy_add(
|
21
|
-
step_method: method,
|
22
|
-
step_args: args,
|
23
|
-
step_block: block,
|
24
|
-
)
|
25
|
-
end
|
14
|
+
# :reek:TooManyStatements
|
15
|
+
def self.parse_step(method:, args:, block:)
|
16
|
+
if args.key?(:if)
|
17
|
+
args[:condition] = args.delete(:if)
|
26
18
|
end
|
27
19
|
|
28
|
-
if
|
29
|
-
|
30
|
-
steppy_add step_if: condition, step_block: block
|
31
|
-
end
|
20
|
+
if args.key?(:unless)
|
21
|
+
args[:condition] = -> { !steppy_run_condition(args.delete(:unless)) }
|
32
22
|
end
|
33
23
|
|
34
|
-
if
|
35
|
-
|
36
|
-
|
37
|
-
step_if: -> { !steppy_run_block(condition) },
|
38
|
-
step_block: block,
|
39
|
-
)
|
40
|
-
end
|
24
|
+
if method.is_a?(Proc)
|
25
|
+
block = method
|
26
|
+
method = nil
|
41
27
|
end
|
42
28
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
29
|
+
{ method: method, args: args, block: block }
|
30
|
+
end
|
31
|
+
|
32
|
+
# Steppy class methods that will be added to your included classes.
|
33
|
+
module ClassMethods
|
34
|
+
def steppy(&block)
|
35
|
+
steppy_cache[:block] = block
|
47
36
|
end
|
48
37
|
|
49
|
-
def
|
50
|
-
|
38
|
+
def step_set(*sets)
|
39
|
+
steppy_cache[:sets] += sets
|
51
40
|
end
|
52
41
|
|
53
|
-
def
|
54
|
-
steppy_cache[:steps]
|
42
|
+
def step(method = nil, args = {}, &block) # rubocop:disable Airbnb/OptArgParameters
|
43
|
+
steppy_cache[:steps].push(
|
44
|
+
Steppy.parse_step({ method: method, args: args, block: block })
|
45
|
+
)
|
46
|
+
self
|
55
47
|
end
|
48
|
+
alias stepy_return step
|
56
49
|
|
57
|
-
def
|
58
|
-
|
59
|
-
|
50
|
+
def step_if(condition, &block)
|
51
|
+
steppy_cache[:steps].push(condition: condition, block: block)
|
52
|
+
self
|
60
53
|
end
|
61
54
|
|
62
|
-
def
|
63
|
-
steppy_cache[:
|
55
|
+
def step_unless(condition, &block)
|
56
|
+
steppy_cache[:steps].push(condition: -> { !steppy_run_condition(condition) }, block: block)
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def step_rescue(exceptions = nil, &block) # rubocop:disable Airbnb/OptArgParameters
|
61
|
+
steppy_cache[:rescues].push(exceptions: exceptions, block: block)
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
def steppy_cache
|
66
|
+
@steppy_cache ||= SteppyCache.new(steps: [], sets: [], rescues: [])
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
67
70
|
# Steppy instance methods that will be added.
|
68
71
|
module InstanceMethods
|
69
|
-
|
70
|
-
@steppy_prefix = prefix
|
71
|
-
@steppy = { attributes: attributes.freeze }
|
72
|
+
attr_reader :steppy_cache
|
72
73
|
|
73
|
-
|
74
|
-
|
75
|
-
)
|
76
|
-
end
|
74
|
+
def steppy(attributes, cache = {})
|
75
|
+
steppy_initialize_cache({ attributes: attributes, prefix: :step }.merge(cache))
|
77
76
|
|
78
|
-
|
79
|
-
|
77
|
+
if steppy_cache.key?(:block)
|
78
|
+
instance_exec(&steppy_cache[:block])
|
79
|
+
else
|
80
|
+
steppy_run(steppy_cache)
|
81
|
+
end
|
82
|
+
rescue => exception
|
83
|
+
steppy_rescue exception, steppy_cache[:rescues]
|
80
84
|
end
|
81
85
|
|
82
|
-
|
86
|
+
def step_set(*sets)
|
87
|
+
steppy_sets(sets)
|
88
|
+
end
|
83
89
|
|
84
|
-
def
|
85
|
-
|
86
|
-
|
90
|
+
def step(method = nil, args = {}, &block) # rubocop:disable Airbnb/OptArgParameters
|
91
|
+
steppy_run_step Steppy.parse_step({ method: method, args: args, block: block })
|
92
|
+
end
|
93
|
+
alias stepy_return step
|
87
94
|
|
88
|
-
|
95
|
+
def step_if(condition, &block)
|
96
|
+
steppy_run_condition_block condition, block
|
89
97
|
end
|
90
98
|
|
91
|
-
def
|
92
|
-
|
93
|
-
block && steppy_class.instance_exec(&block)
|
99
|
+
def step_unless(condition, &block)
|
100
|
+
steppy_run_condition_block -> { !steppy_run_condition(condition) }, block
|
94
101
|
end
|
95
102
|
|
96
|
-
def
|
97
|
-
|
103
|
+
def step_rescue(*)
|
104
|
+
raise '#step_rescue can not be used in a block, please just add rescue to the #steppy block.'
|
98
105
|
end
|
99
106
|
|
100
|
-
|
101
|
-
sets ||= steppy_class_cache[:sets]
|
107
|
+
protected
|
102
108
|
|
103
|
-
|
104
|
-
|
105
|
-
|
109
|
+
def steppy_run(steps:, sets:, **)
|
110
|
+
steppy_sets(sets)
|
111
|
+
steppy_steps(steps)
|
106
112
|
end
|
107
113
|
|
108
|
-
def
|
109
|
-
|
114
|
+
def steppy_sets(sets)
|
115
|
+
sets.each { |key, value| steppy_set(key, value || steppy_attributes[key]) }
|
116
|
+
end
|
110
117
|
|
111
|
-
|
112
|
-
|
113
|
-
elsif step_method.is_a? Proc
|
114
|
-
steppy_run_block(step_method)
|
115
|
-
else
|
116
|
-
steppy_step(step)
|
117
|
-
end
|
118
|
+
def steppy_set(key, value)
|
119
|
+
key && instance_variable_set("@#{key}", value)
|
118
120
|
end
|
119
121
|
|
120
|
-
def
|
121
|
-
|
122
|
+
def steppy_steps(steps)
|
123
|
+
steps.each do |step|
|
124
|
+
condition = step[:condition]
|
122
125
|
|
123
|
-
|
126
|
+
steppy_cache[:result] = if condition
|
127
|
+
steppy_run_condition_block(condition, step[:block])
|
128
|
+
else
|
129
|
+
steppy_run_step(step)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
steppy_result
|
124
134
|
end
|
125
135
|
|
126
|
-
def
|
127
|
-
|
128
|
-
|
136
|
+
def steppy_rescue(exception, rescues)
|
137
|
+
exception_class = exception.class
|
138
|
+
|
139
|
+
if exception_class == SteppyError || rescues.empty?
|
140
|
+
raise exception
|
129
141
|
end
|
130
142
|
|
131
|
-
|
143
|
+
rescues.each do |exceptions:, block:|
|
144
|
+
if !exceptions || (exceptions && !exceptions.include?(exception_class))
|
145
|
+
steppy_cache[:result] = instance_exec(steppy_attributes, &block)
|
146
|
+
end
|
147
|
+
end
|
132
148
|
|
133
|
-
|
134
|
-
|
135
|
-
else
|
136
|
-
steppy_run_method(method_name)
|
137
|
-
end
|
149
|
+
steppy_result
|
150
|
+
end
|
138
151
|
|
139
|
-
|
152
|
+
def steppy_run_condition_block(condition, block)
|
153
|
+
if steppy_run_condition(condition)
|
154
|
+
steppy_run(steppy_cache_from_block(block))
|
155
|
+
end
|
140
156
|
end
|
141
157
|
|
142
|
-
def
|
143
|
-
|
144
|
-
|
158
|
+
def steppy_run_condition(condition)
|
159
|
+
return true unless condition
|
160
|
+
|
161
|
+
if condition.arity > 0
|
162
|
+
instance_exec(steppy_attributes, &condition)
|
145
163
|
else
|
146
|
-
|
164
|
+
instance_exec(&condition)
|
147
165
|
end
|
148
166
|
end
|
149
167
|
|
150
|
-
def
|
151
|
-
|
152
|
-
|
168
|
+
def steppy_run_step(method:, args:, block:)
|
169
|
+
return unless steppy_run_condition(args[:condition])
|
170
|
+
|
171
|
+
result = if block
|
172
|
+
instance_exec(steppy_attributes, &block)
|
153
173
|
else
|
154
|
-
|
174
|
+
steppy_run_method(method, steppy_attributes)
|
155
175
|
end
|
176
|
+
|
177
|
+
steppy_set(args[:set], result)
|
178
|
+
|
179
|
+
result
|
156
180
|
end
|
157
181
|
|
158
|
-
def
|
159
|
-
|
182
|
+
def steppy_run_method(method_name, attributes)
|
183
|
+
method = "#{steppy_cache[:prefix]}_#{method_name}"
|
160
184
|
|
161
|
-
if
|
162
|
-
|
185
|
+
if method(method).arity > 0
|
186
|
+
public_send(method, attributes)
|
163
187
|
else
|
164
|
-
|
188
|
+
public_send(method)
|
165
189
|
end
|
166
190
|
end
|
167
191
|
|
168
|
-
def
|
169
|
-
|
170
|
-
|
171
|
-
result
|
192
|
+
def steppy_cache_from_block(block)
|
193
|
+
Class.new { include Steppy }.instance_exec(&block).steppy_cache
|
172
194
|
end
|
173
195
|
|
174
|
-
def
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
end
|
196
|
+
def steppy_initialize_cache(cache)
|
197
|
+
@steppy_cache = SteppyCache.new(
|
198
|
+
self.class.steppy_cache.to_h.merge(result: nil).merge(cache)
|
199
|
+
)
|
200
|
+
end
|
180
201
|
|
181
|
-
|
202
|
+
def steppy_attributes
|
203
|
+
steppy_cache[:attributes]
|
182
204
|
end
|
183
205
|
|
184
|
-
def
|
185
|
-
|
206
|
+
def steppy_result
|
207
|
+
steppy_cache[:result]
|
186
208
|
end
|
187
209
|
end
|
188
210
|
end
|
data/lib/steppy/cache.rb
CHANGED
data/lib/steppy/error.rb
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
|
3
3
|
# Incase you need to throw an error related to steppy
|
4
4
|
class SteppyError < StandardError
|
5
|
-
attr_reader :
|
5
|
+
attr_reader :steppy
|
6
6
|
# rubocop:disable Airbnb/OptArgParameters
|
7
|
-
def initialize(
|
7
|
+
def initialize(steppy = nil)
|
8
8
|
# rubocop:enable Airbnb/OptArgParameters
|
9
|
-
if
|
10
|
-
@
|
11
|
-
message =
|
9
|
+
if steppy
|
10
|
+
@steppy = steppy
|
11
|
+
message = steppy.is_a?(String) ? steppy : steppy.to_json
|
12
12
|
end
|
13
13
|
|
14
14
|
super(message)
|
data/lib/steppy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: steppy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- cj
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|