railway_operation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f98482d3c4408b13a570eb7ef7aa82ed476d202
4
+ data.tar.gz: 50c63207bc89ab21e457a5103e0a1fb59fc1a565
5
+ SHA512:
6
+ metadata.gz: 9ebde675b7cd14b824bd6ee2ef6f9038418ce28fcb728a12e8290faaa76f68d81f5dff45eff9c41ea3086aaa40ffa441ed2a26726a0a657440bfe633aca59b44
7
+ data.tar.gz: f01a2016c484614e6a3563f4b3b7b1cd2ae290b972b7650caf89ef1ccc7a0c316514ee195d277112af73d7f92fa3369d50bc9452658d0992ee675df4e91911d3
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,31 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ Gemspec/OrderedDependencies:
4
+ Exclude:
5
+ - 'railway_operation.gemspec'
6
+
7
+ Style/FrozenStringLiteralComment:
8
+ Exclude:
9
+ - 'Gemfile'
10
+ - 'bin/console'
11
+
12
+ Metrics/BlockLength:
13
+ Enabled: false
14
+ Metrics/LineLength:
15
+ Enabled: false
16
+ Metrics/MethodLength:
17
+ Enabled: false
18
+ Style/DoubleNegation:
19
+ Enabled: false
20
+ Style/IfUnlessModifier:
21
+ Enabled: false
22
+ Style/Lambda:
23
+ Enabled: false
24
+ Style/LambdaCall:
25
+ Enabled: false
26
+ Style/SymbolArray:
27
+ Enabled: false
28
+ Style/GuardClause:
29
+ Enabled: false
30
+ Style/WordArray:
31
+ Enabled: false
@@ -0,0 +1,73 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2018-03-10 19:36:36 -0500 using RuboCop version 0.52.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ Lint/Void:
11
+ Exclude:
12
+ - 'lib/railway_operation/filled_matrix.rb'
13
+
14
+ # Offense count: 3
15
+ Metrics/AbcSize:
16
+ Max: 41
17
+
18
+ # Offense count: 1
19
+ Metrics/CyclomaticComplexity:
20
+ Max: 9
21
+
22
+ # Offense count: 1
23
+ Metrics/PerceivedComplexity:
24
+ Max: 10
25
+
26
+ # Offense count: 1
27
+ # Cop supports --auto-correct.
28
+ # Configuration parameters: EnforcedStyle.
29
+ # SupportedStyles: braces, no_braces, context_dependent
30
+ Style/BracesAroundHashParameters:
31
+ Exclude:
32
+ - 'lib/railway_operation/operator.rb'
33
+
34
+ # Offense count: 5
35
+ Style/Documentation:
36
+ Exclude:
37
+ - 'spec/**/*'
38
+ - 'test/**/*'
39
+ - 'lib/railway_operation.rb'
40
+ - 'lib/railway_operation/filled_matrix.rb'
41
+ - 'lib/railway_operation/operator.rb'
42
+ - 'lib/railway_operation/surround.rb'
43
+ - 'lib/railway_operation/visualizer.rb'
44
+
45
+ # Offense count: 1
46
+ # Cop supports --auto-correct.
47
+ # Configuration parameters: EnforcedStyle.
48
+ # SupportedStyles: compact, exploded
49
+ Style/RaiseArgs:
50
+ Exclude:
51
+ - 'lib/railway_operation/operator.rb'
52
+
53
+ # Offense count: 1
54
+ # Cop supports --auto-correct.
55
+ # Configuration parameters: EnforcedStyle.
56
+ # SupportedStyles: implicit, explicit
57
+ Style/RescueStandardError:
58
+ Exclude:
59
+ - 'lib/railway_operation/operator.rb'
60
+
61
+ # Offense count: 3
62
+ # Cop supports --auto-correct.
63
+ # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
64
+ # SupportedStyles: single_quotes, double_quotes
65
+ Style/StringLiterals:
66
+ Exclude:
67
+ - 'bin/console'
68
+
69
+ # Offense count: 13
70
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
71
+ # URISchemes: http, https
72
+ Metrics/LineLength:
73
+ Max: 105
@@ -0,0 +1 @@
1
+ 2.4.2
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install:
6
+ - gem install bundler -v 1.14.6
7
+ - "sudo apt-get install graphviz"
8
+ script:
9
+ - bundle exec rspec spec
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at felixflores@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in railway_operation.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Felix Flores
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,326 @@
1
+ [![Build Status](https://travis-ci.org/felixflores/railway_operation.svg?branch=master)](https://travis-ci.org/felixflores/railway_operation)
2
+
3
+ # RailwayOperation
4
+
5
+ This gem allows you to declare and compose a set of operations into a functional execution tree inspired by the railway oriented programming pattern. See ([https://fsharpforfunandprofit.com/rop/](https://fsharpforfunandprofit.com/rop/)) for more details.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'railway_operation'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install railway_operation
22
+
23
+ Then in any of your ruby class `include RailwayOperation::Operator`.
24
+
25
+ ## Basic Usage
26
+ Let's say we have the following class
27
+
28
+ ```ruby
29
+ module Readme
30
+ class Example1
31
+ def initialize(someone = 'someone')
32
+ @someone = someone
33
+ end
34
+
35
+ def first_method(argument)
36
+ argument << "Hello #{@someone}, from first_method."
37
+ end
38
+
39
+ def another_method(argument)
40
+ argument << 'Hello from another_method.'
41
+ end
42
+
43
+ def final_method(argument)
44
+ argument << 'Hello from final_method.'
45
+ end
46
+ end
47
+ end
48
+ ```
49
+
50
+ We could perform the follow chain of execution, to yield the following result.
51
+
52
+ ```ruby
53
+ ex1 = Readme::Example1.new('Felix')
54
+
55
+ argument = []
56
+ argument = ex1.first_method(argument)
57
+ argument = ex1.another_method(argument)
58
+ result = ex1.final_method(argument)
59
+
60
+ result == [
61
+ 'Hello Felix, from first_method.'
62
+ 'Hello from another_method.'
63
+ 'Hello from final_method.'
64
+ ]
65
+ ```
66
+
67
+ RailwayOperation provides a way for you to declare the same execution chain as a series of steps in an operation.
68
+
69
+ If we add the following
70
+
71
+ ```ruby
72
+ module Readme
73
+ class Example1
74
+ include RailwayOperation
75
+ ```
76
+
77
+ to your class, we can then declare an operation block
78
+
79
+ ```ruby
80
+ operation do |o|
81
+ o.add_step 1, :first_method
82
+ o.add_step 1, :another_method
83
+ o.add_step 1, :final_method
84
+ end
85
+ ```
86
+
87
+ Before we can take advantage of RailwayOperation we need to modify our method signatures slightly from `def first_method(argument)` to `def first_method(arugment, **)`. This allows our methods to accept an addtional has called info (we will cover this topic of `info` in more detail shortly)
88
+
89
+ [./spec/readme/example_1\_spec.rb](https://github.com/felixflores/railway_operation/blob/master/spec/readme/example_1_spec.rb)
90
+
91
+ ```ruby
92
+ module Readme
93
+ class Example1
94
+ include RailwayOperation
95
+
96
+ operation do |o|
97
+ o.add_step 1, :first_method
98
+ o.add_step 1, :another_method
99
+ o.add_step 1, :final_method
100
+ end
101
+
102
+ def initialize(someone = 'someone')
103
+ @someone = someone
104
+ end
105
+
106
+ def first_method(argument, **)
107
+ argument << "Hello #{@someone}, from first_method."
108
+ end
109
+
110
+ def another_method(argument, **)
111
+ argument << 'Hello from another_method.'
112
+ end
113
+
114
+ def final_method(argument, **)
115
+ argument << 'Hello from final_method.'
116
+ end
117
+ end
118
+ end
119
+ ```
120
+
121
+ Now we can call the `.run` method on the class to yeild the same result.
122
+
123
+ ```ruby
124
+ arugment = []
125
+ result, info = Readme::Example1.new('Felix').run(argument)
126
+
127
+ result == [
128
+ 'Hello Felix, from first_method.'
129
+ 'Hello from another_method.'
130
+ 'Hello from final_method.'
131
+ ]
132
+ ```
133
+
134
+ Additionally, if your class does not require any arguments in its initializer you can call.
135
+
136
+ ```ruby
137
+ result, info = Readme::Example1.run(argument)
138
+
139
+ result == [
140
+ 'Hello someone, from first_method.',
141
+ 'Hello from another_method.',
142
+ 'Hello from final_method.'
143
+ ]
144
+ ```
145
+
146
+ One important detail to call out here is that calling run returns the `result` object (which is the return value of the operation) and an `info` object which is a hash like object containing information about the execution of the operation. To see a brief overview of the types of information `info` see [./spec/readme/example\_1_spec.rb](https://github.com/felixflores/railway_operation/blob/master/spec/readme/example_1_spec.rb)
147
+
148
+ A more detailed explanation of `info` is on the [RailwayOperation: Info](https://github.com/felixflores/railway_operation#info) section.
149
+
150
+ ## Multitrack Execution
151
+
152
+ ### Stepper Function
153
+ So far we've seen a single track execution of an operation. The track is the first argument of the `add_step` method. In our previous example all our steps executed on track 1.
154
+
155
+ ![basic - page 1](https://user-images.githubusercontent.com/65030/38450687-5067bd94-39f0-11e8-9b85-198ba7b28b1b.png)
156
+
157
+
158
+ Let's now consider the following example
159
+
160
+ ```ruby
161
+ module Readme
162
+ class Example2_1
163
+ include RailwayOperation
164
+
165
+ operation do |o|
166
+ o.add_step 1, :method_1
167
+ o.add_step 1, :method_2
168
+ o.add_step 2, :method_3
169
+ o.add_step 2, :method_4
170
+ end
171
+
172
+ def initialize(someone = 'someone')
173
+ @someone = someone
174
+ end
175
+
176
+ def method_1(argument, **)
177
+ argument << 1
178
+ end
179
+
180
+ def method_2(argument, **)
181
+ argument << 2
182
+ end
183
+
184
+ def method_3(argument, **)
185
+ argument << 3
186
+ end
187
+
188
+ def method_4(argument, **)
189
+ argument << 4
190
+ end
191
+ end
192
+ end
193
+ ```
194
+
195
+ When we invoke `run` this we'll get the following result.
196
+
197
+ ```ruby
198
+ result, _info = Readme::Example2_1.run([])
199
+ result == [1, 2]
200
+ ```
201
+
202
+ What happened here? Instead of `method_3` and `method_4` being on track one, they are now set to execute on track 2. So when we ran the operation it only ran the methods on track one.
203
+
204
+ ![example 2 1 - page 1 1](https://user-images.githubusercontent.com/65030/38447196-f4b65800-39c9-11e8-94fc-310c4931d7fb.png)
205
+
206
+ In order to change the execution path of the operation to track 2 we need to introduce a new concept called `stepper_function`. The `stepper_function` is responsible for executing each step of the operation and deciding the direction of next step of the operation.
207
+
208
+ ```
209
+ operation do |o|
210
+ o.stepper_function do |stepper, _, &step|
211
+ argument, _ = step.call
212
+
213
+ if argument.length >= 2
214
+ stepper.switch_to(2)
215
+ end
216
+
217
+ stepper.continue
218
+ end
219
+
220
+ o.add_step 1, :method_1
221
+ o.add_step 1, :method_2
222
+ o.add_step 2, :method_3
223
+ o.add_step 2, :method_4
224
+ end
225
+ ```
226
+
227
+ Now, when we call run we get the folling result.
228
+
229
+ ```ruby
230
+ argument = []
231
+ result, _info = Readme::Example2_2.run(argument)
232
+ result == [1, 2, 3, 4]
233
+ ```
234
+
235
+ ![example 2 2 - page 1](https://user-images.githubusercontent.com/65030/38449378-5b1c6ac8-39dc-11e8-9cf9-f9e5c1a40cb6.png)
236
+
237
+ For now you can think of the `stepper_function` as a `lambda` that surrounds a step (this is not entirely accurate, but it's good enough for now). This `lambda` has the following shape.
238
+
239
+ ```ruby
240
+ lambda do |stepper, info, &step|
241
+
242
+ ...
243
+
244
+ end
245
+ ```
246
+
247
+ The `stepper` argument is control structure that dictates the movement of the execution.
248
+
249
+ ```ruby
250
+ stepper.continue
251
+ stepper.switch_to(specified_track)
252
+ stepper.successor_track
253
+ stepper.halt_operation
254
+ stepper.fail_operation
255
+ ```
256
+ In our example we used the `switch_to` and `continue` methods to switch from track 1 to 2 and continue the execution of our operation.
257
+
258
+ The `info` argument is the same `info` object we've seen from calling `run`, it is passed from one step to another.
259
+
260
+ Finally, `step` is a `lambda` which runs the step once called. Overlayed on top of our previous diagram, it would roughly look like this.
261
+
262
+ ![example 2 2 info - page 1 2](https://user-images.githubusercontent.com/65030/38457951-3c76074e-3a65-11e8-9243-3dfe412dd645.png)
263
+
264
+ To overlay the `stepper_function` in our example more concretely, looks something like this.
265
+
266
+ ![example 2 2 decisions - page 1 3](https://user-images.githubusercontent.com/65030/38458117-270bbb80-3a68-11e8-8e48-ecfba0e8512b.png)
267
+
268
+ This process is recursed until the highest index step in the operation is reached. In this case the `operation.last_step_index` is 3. When the final step recursion is reach the value of the argument and info at that point is returned as the result of the operation.
269
+
270
+ ### Track Alias
271
+
272
+ Another important concept for multi-tract execution is the idea of track alias. Instead of simply relying on the ordinal track index of 1, 2, 3, and so on, we can instead map those indices to a symbol.
273
+
274
+
275
+ ```ruby
276
+ operation do |o|
277
+ o.tracks :track1, :track2
278
+
279
+ o.stepper_function do |stepper, _, &step|
280
+ # this returns result, info identical to how
281
+ # calling run returns result and info
282
+ argument, _ = step.call
283
+
284
+ if argument.length >= 2
285
+ stepper.switch_to(2)
286
+ end
287
+
288
+ stepper.continue
289
+ end
290
+
291
+ o.add_step :track1, :method_1
292
+ o.add_step :track1, :method_2
293
+ o.add_step :track2, :method_3
294
+ o.add_step :track2, :method_4
295
+ end
296
+
297
+ ```
298
+
299
+ ![example 2 3 - page 1](https://user-images.githubusercontent.com/65030/38451877-42d5d24a-3a06-11e8-9b94-dc6ec9f0edc9.png)
300
+
301
+ `stepper_function` and `track_alias` combine are known as a strategy. The next section will dig into this more deeply.
302
+
303
+ ## Strategy
304
+ ![strategy - page 1](https://user-images.githubusercontent.com/65030/38476310-004026b2-3b7b-11e8-8f63-f96ef5f3fe1e.png)
305
+
306
+
307
+
308
+ ## Info
309
+
310
+ TODO
311
+
312
+ ## Development
313
+
314
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
315
+
316
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
317
+
318
+ ## Contributing
319
+
320
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/railway_operation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
321
+
322
+
323
+ ## License
324
+
325
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
326
+