steppy 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a3e69bd0bf6da2bc6ad68601777f7aaf6c350afabf6f899db8dcb244ea9ebcf
4
- data.tar.gz: c2e3b695f6f791c3c90c523c357915071fe0a5cff19151d10ca4bed3b795e3b0
3
+ metadata.gz: a9fa807ba5d30abc006adf68842988e5fb4fb48a77407fc3b34ba92494a375ba
4
+ data.tar.gz: 79da4b077d8c45dcd60486831bd29242446533cb7d7f3a9b32d351ef47683d88
5
5
  SHA512:
6
- metadata.gz: c694feb7e483edbee03a84d936f5d64295911c318232ec31ea6f7ba9b58068d199cec2df97e1f9f14284387dd01c0e0c56b20f18e7c61d63ddfcac668110dbb0
7
- data.tar.gz: 4865f642107ed0cfd33d1c5ac2200cdeef40adc71407e73f9cd349f33994c556fcc65f44556faa66b7000a7c3a199496bfed05297087b05bb305fa97692854e3
6
+ metadata.gz: d080019e5966955f4278671c4ec3400268048781c4c3b7c2e2b67fea2fbe3e8c953583f0848cb3c7a2f7eb086b8d99c72d6f3042e0c7cadaafb6e55743796db3
7
+ data.tar.gz: b3466168ec557ce684a226b1bc516f54736dc2d9f80d062ee599199e943b637dfa2921f20b849a52d2265edc3ee8578f7279b9e5cf8438816d7479a17d655cf8
@@ -22,3 +22,6 @@ Airbnb/ClassOrModuleDeclaredInWrongFile:
22
22
 
23
23
  Airbnb/ModuleMethodInWrongFile:
24
24
  Enabled: false
25
+
26
+ Lint/EndAlignment:
27
+ EnforcedStyleAlignWith: variable
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- steppy (0.6.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.0)
27
+ parser (2.5.1.2)
28
28
  ast (~> 2.4.0)
29
- powerpack (0.1.1)
29
+ powerpack (0.1.2)
30
30
  rainbow (3.0.0)
31
31
  rake (10.5.0)
32
- reek (4.8.1)
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.9.0)
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.3.2)
55
+ unicode-display_width (1.4.0)
56
56
  virtus (1.0.5)
57
57
  axiom-types (~> 0.1)
58
58
  coercible (~> 1.0)
@@ -11,178 +11,200 @@ module Steppy
11
11
  base.include InstanceMethods
12
12
  end
13
13
 
14
- # Steppy class methods that will be added to your included classes.
15
- module ClassMethods
16
- if !defined? step
17
- def step(method, **args, &block)
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 !defined? step_if
29
- def step_if(condition, &block)
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 !defined? step_unless
35
- def step_unless(condition, &block)
36
- steppy_add(
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
- if !defined? step_set
44
- def step_set(*sets)
45
- steppy_cache[:sets] = *sets
46
- end
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 steppy_cache
50
- @steppy_cache ||= SteppyCache.new(steps: nil, sets: [], block: nil)
38
+ def step_set(*sets)
39
+ steppy_cache[:sets] += sets
51
40
  end
52
41
 
53
- def steppy_steps
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 steppy_add(step)
58
- steppy_steps.push(step)
59
- steppy_cache
50
+ def step_if(condition, &block)
51
+ steppy_cache[:steps].push(condition: condition, block: block)
52
+ self
60
53
  end
61
54
 
62
- def steppy(&block)
63
- steppy_cache[:block] = block
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
- def steppy(attributes, prefix: :step)
70
- @steppy_prefix = prefix
71
- @steppy = { attributes: attributes.freeze }
72
+ attr_reader :steppy_cache
72
73
 
73
- steppy_run(
74
- (steppy_class_block || steppy_class_cache).to_h
75
- )
76
- end
74
+ def steppy(attributes, cache = {})
75
+ steppy_initialize_cache({ attributes: attributes, prefix: :step }.merge(cache))
77
76
 
78
- def steppy_attributes
79
- @steppy[:attributes]
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
- protected
86
+ def step_set(*sets)
87
+ steppy_sets(sets)
88
+ end
83
89
 
84
- def steppy_run(steps:, sets:, **)
85
- steppy_run_sets(sets)
86
- steppy_run_steps(steps)
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
- @steppy[:result] || nil
95
+ def step_if(condition, &block)
96
+ steppy_run_condition_block condition, block
89
97
  end
90
98
 
91
- def steppy_class_block
92
- block = steppy_class_cache[:block]
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 steppy_class_cache
97
- self.class.steppy_cache
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
- def steppy_run_sets(sets)
101
- sets ||= steppy_class_cache[:sets]
107
+ protected
102
108
 
103
- sets.each do |set|
104
- steppy_set(set, steppy_attributes[set])
105
- end
109
+ def steppy_run(steps:, sets:, **)
110
+ steppy_sets(sets)
111
+ steppy_steps(steps)
106
112
  end
107
113
 
108
- def steppy_run_step(step)
109
- step_method = step[:step_method]
114
+ def steppy_sets(sets)
115
+ sets.each { |key, value| steppy_set(key, value || steppy_attributes[key]) }
116
+ end
110
117
 
111
- if step.key?(:step_if)
112
- steppy_if_block(step)
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 steppy_run_steps(steps)
121
- return if !steps
122
+ def steppy_steps(steps)
123
+ steps.each do |step|
124
+ condition = step[:condition]
122
125
 
123
- steps.each { |step| @steppy[:result] = steppy_run_step(step) }
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 steppy_step(step_method:, step_args:, step_block:)
127
- if steppy_if(step_args[:if]) || @steppy_prefix != step_args[:prefix]
128
- return
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
- method_name = "#{@steppy_prefix}_#{step_method}"
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
- result = if step_block
134
- steppy_run_block(step_block)
135
- else
136
- steppy_run_method(method_name)
137
- end
149
+ steppy_result
150
+ end
138
151
 
139
- steppy_set(step_args[:set], result)
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 steppy_run_method(method_name)
143
- if method(method_name).arity > 0
144
- public_send(method_name, steppy_attributes)
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
- public_send(method_name)
164
+ instance_exec(&condition)
147
165
  end
148
166
  end
149
167
 
150
- def steppy_run_block(step_block)
151
- if step_block.arity > 0
152
- instance_exec(steppy_attributes, &step_block)
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
- instance_exec(&step_block)
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 steppy_if(step_if)
159
- return unless step_if
182
+ def steppy_run_method(method_name, attributes)
183
+ method = "#{steppy_cache[:prefix]}_#{method_name}"
160
184
 
161
- if step_if.arity > 0
162
- !instance_exec(steppy_attributes, &step_if)
185
+ if method(method).arity > 0
186
+ public_send(method, attributes)
163
187
  else
164
- !instance_exec(&step_if)
188
+ public_send(method)
165
189
  end
166
190
  end
167
191
 
168
- def steppy_set(step_set, result)
169
- step_set && instance_variable_set("@#{step_set}", result)
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 steppy_if_block(step_if:, step_block:)
175
- passed = if step_if.arity > 0
176
- instance_exec(steppy_attributes, &step_if)
177
- else
178
- instance_exec(&step_if)
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
- passed && steppy_run(steppy_class.instance_exec(&step_block).to_h)
202
+ def steppy_attributes
203
+ steppy_cache[:attributes]
182
204
  end
183
205
 
184
- def steppy_class
185
- Class.new { include Steppy }
206
+ def steppy_result
207
+ steppy_cache[:result]
186
208
  end
187
209
  end
188
210
  end
@@ -24,5 +24,9 @@ module Steppy
24
24
  def to_h
25
25
  @mutex.synchronize { @hash }
26
26
  end
27
+
28
+ def method_missing(method, *args, &block)
29
+ @mutex.synchronize { @hash.public_send(method, *args, &block) }
30
+ end
27
31
  end
28
32
  end
@@ -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 :step
5
+ attr_reader :steppy
6
6
  # rubocop:disable Airbnb/OptArgParameters
7
- def initialize(step = nil)
7
+ def initialize(steppy = nil)
8
8
  # rubocop:enable Airbnb/OptArgParameters
9
- if step
10
- @step = step
11
- message = step.to_json
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Steppy
4
- VERSION = '0.6.0'
4
+ VERSION = '0.7.0'
5
5
  end
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.6.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-13 00:00:00.000000000 Z
11
+ date: 2018-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler