trailblazer-activity 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +67 -21
- data/lib/trailblazer/activity.rb +5 -5
- data/lib/trailblazer/activity/graph.rb +0 -2
- data/lib/trailblazer/{circuit → activity}/present.rb +1 -1
- data/lib/trailblazer/{circuit → activity}/trace.rb +5 -5
- data/lib/trailblazer/activity/version.rb +1 -1
- data/lib/trailblazer/{circuit → activity}/wrap.rb +5 -5
- data/lib/trailblazer/circuit/testing.rb +2 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 767263f9c44b98ac587ae990b3feed3ac74f11ed
|
4
|
+
data.tar.gz: 0ef9c2e37f99170067b35ee8296705459bf9a9df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e6a9385a56bbbae8747836e5946c3d6fb8966fc50377ec4fc55c5ffb6324a8a435b8bdbbec3fa5f107f32d76410cc556a795885839b67e14fdef9b9e320e023
|
7
|
+
data.tar.gz: f6ffca410ecc638323d98ac4f2005491431faf3656f914984834ec1e14d4e12b8be516617404feb6b4fcf391cab4cb3849b6ac4f871b85fd15987ac7cf344c82
|
data/README.md
CHANGED
@@ -8,9 +8,7 @@ Circuit refrains from implementing deciders. The decisions are encoded in the ou
|
|
8
8
|
|
9
9
|
`Circuit` and `workflow` use [BPMN](http://www.bpmn.org/) lingo and concepts for describing processes and flows. This document can be found in the [Trailblazer documentation](http://trailblazer.to/gems/workflow/circuit.html), too.
|
10
10
|
|
11
|
-
|
12
|
-
The `circuit` gem is the lowest level of abstraction and is used in `operation` and `workflow`, which both provide higher-level APIs for the Railway pattern and complex BPMN workflows.
|
13
|
-
{% endcallout %}
|
11
|
+
> The `circuit` gem is the lowest level of abstraction and is used in `operation` and `workflow`, which both provide higher-level APIs for the Railway pattern and complex BPMN workflows.
|
14
12
|
|
15
13
|
## Installation
|
16
14
|
|
@@ -26,7 +24,7 @@ The `trailblazer-circuit` gem is often just called the `circuit` gem. It ships w
|
|
26
24
|
|
27
25
|
The following diagram illustrates a common use-case for `circuit`, the task of publishing a blog post.
|
28
26
|
|
29
|
-
<img src="/images/diagrams/blog-bpmn1.png">
|
27
|
+
<img src="http://trailblazer.to/images/diagrams/blog-bpmn1.png">
|
30
28
|
|
31
29
|
After writing and spell-checking, the author has the chance to publish the post or, in case of typos, go back, correct, and go through the same flow, again. Note that there's only a handful of defined transistions, or connections. An author, for example, is not allowed to jump from "correct" into "publish" without going through the check.
|
32
30
|
|
@@ -38,7 +36,17 @@ Your job is solely to implement the tasks and deciders put into this activity -
|
|
38
36
|
|
39
37
|
In order to define an activity, you can use the BPMN editor of your choice and run it through the Trailblazer circuit generator, use our online tool (if [you're a PRO member](http://pro.trailblazer.to)) or simply define it using plain Ruby.
|
40
38
|
|
41
|
-
|
39
|
+
```ruby
|
40
|
+
activity = Activity.from_hash do |start, _end|
|
41
|
+
{
|
42
|
+
start => { Circuit::Right => Blog::Write },
|
43
|
+
Blog::Write => { Circuit::Right => Blog::SpellCheck },
|
44
|
+
Blog::SpellCheck => { Circuit::Right => Blog::Publish, Circuit::Left => Blog::Correct },
|
45
|
+
Blog::Correct => { Circuit::Right => Blog::SpellCheck },
|
46
|
+
Blog::Publish => { Circuit::Right => _end }
|
47
|
+
}
|
48
|
+
end
|
49
|
+
```
|
42
50
|
|
43
51
|
The `Activity` function is a convenient tool to create an activity. Note that the yielded object allows to access *events* from the activity, such as the `Start` and `End` event that are created per default.
|
44
52
|
|
@@ -48,7 +56,14 @@ This defines the control flow - the next step is to actually implement the tasks
|
|
48
56
|
|
49
57
|
A *task* usually maps to a particular box in your diagram. Its API is very simple: a task needs to expose a `call` method, allowing it to be a lambda or any other callable object.
|
50
58
|
|
51
|
-
|
59
|
+
```ruby
|
60
|
+
module Blog
|
61
|
+
Write = ->(direction, options, *flow) do
|
62
|
+
options[:content] = options[:content].strip
|
63
|
+
[ Circuit::Right, options, *flow ]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
52
67
|
|
53
68
|
It receives all arguments returned from the task run before. This means a task should return everything it receives.
|
54
69
|
|
@@ -58,19 +73,27 @@ The first return value is crucial: it dictates what will be the next step when e
|
|
58
73
|
|
59
74
|
For example, the `SpellCheck` task needs to decide which route to take.
|
60
75
|
|
61
|
-
|
76
|
+
```ruby
|
77
|
+
SpellCheck = ->(direction, options, *flow) do
|
78
|
+
direction = SpellChecker.error_count(options[:content]) ? Circuit::Right : Circuit::Left
|
79
|
+
[ direction, options, *flow ]
|
80
|
+
end
|
81
|
+
```
|
62
82
|
|
63
83
|
It's as simple as returning the appropriate signal.
|
64
84
|
|
65
|
-
|
66
|
-
You can use any object as a direction signal and return it, as long as it's defined in the circuit.
|
67
|
-
{% endcallout %}
|
85
|
+
> You can use any object as a direction signal and return it, as long as it's defined in the circuit.
|
68
86
|
|
69
87
|
## Call
|
70
88
|
|
71
89
|
After defining circuit and implementing the tasks, the circuit can be executed using its very own `call` method.
|
72
90
|
|
73
|
-
|
91
|
+
```ruby
|
92
|
+
direction, options, flow = activity.(
|
93
|
+
nil,
|
94
|
+
{ content: "Let's start writing " } # gets trimmed in Write.
|
95
|
+
)
|
96
|
+
```
|
74
97
|
|
75
98
|
The first argument is where to start the circuit. Usually, this will be the activity's `Start` event accessable via `activity[:Start]`.
|
76
99
|
|
@@ -78,7 +101,10 @@ All options are passed straight to the first task, which in turn has to make sur
|
|
78
101
|
|
79
102
|
The activity's return set is the last run task and all arguments from the last task.
|
80
103
|
|
81
|
-
|
104
|
+
```ruby
|
105
|
+
direction #=> #<End: default {}>
|
106
|
+
options #=> {:content=>"Let's start writing"}
|
107
|
+
```
|
82
108
|
|
83
109
|
As opposed to higher abstractions such as `Operation`, it is completely up to the developer what interfaces they provide to tasks and their return values. What is a mutable hash here could be an explicit array of return values in another implementation style, and so on.
|
84
110
|
|
@@ -86,25 +112,45 @@ As opposed to higher abstractions such as `Operation`, it is completely up to th
|
|
86
112
|
|
87
113
|
For debugging or simply understanding the flows of circuits, you can use tracing.
|
88
114
|
|
89
|
-
|
115
|
+
```ruby
|
116
|
+
activity = Activity.from_hash do |start, _end|
|
117
|
+
# Blog::Write=>"Blog::Write",Blog::SpellCheck=>"Blog::SpellCheck",Blog::Correct=>"Blog::Correct", Blog::Publish=>"Blog::Publish" }) { |evt|
|
118
|
+
{
|
119
|
+
start => { Circuit::Right => Blog::Write },
|
120
|
+
Blog::Write => { Circuit::Right => Blog::SpellCheck },
|
121
|
+
Blog::SpellCheck => { Circuit::Right => Blog::Publish, Circuit::Left => Blog::Correct },
|
122
|
+
Blog::Correct => { Circuit::Right => Blog::SpellCheck },
|
123
|
+
Blog::Publish => { Circuit::Right => _end }
|
124
|
+
}
|
125
|
+
end
|
126
|
+
```
|
90
127
|
|
91
128
|
The second argument to `Activity` takes debugging information, so you can set readable names for tasks.
|
92
129
|
|
93
130
|
When invoking the activity, the `:runner` option will activate tracing and write debugging information about any executed task onto the `:stack` array.
|
94
131
|
|
95
|
-
|
132
|
+
```ruby
|
133
|
+
stack, _ = Circuit::Trace.( activity,
|
134
|
+
nil,
|
135
|
+
{ content: "Let's start writing" }
|
136
|
+
)
|
137
|
+
```
|
96
138
|
|
97
139
|
The `stack` can then be passed to a presenter.
|
98
140
|
|
99
|
-
|
141
|
+
```
|
142
|
+
Circuit::Trace::Present.tree(stack)
|
143
|
+
|--> #<Start: default {}>{:content=>"Let's start writing"}
|
144
|
+
|--> Blog::Write{:content=>"Let's start writing"}
|
145
|
+
|--> Blog::SpellCheck{:content=>"Let's start writing"}
|
146
|
+
|--> Blog::Publish{:content=>"Let's start writing"}
|
147
|
+
`--> #<End: default {}>{:content=>"Let's start writing"}
|
148
|
+
```
|
100
149
|
|
101
150
|
Tracing is extremely efficient to find out what is going wrong and supersedes cryptic debuggers by many times. Note that tracing also works for deeply nested circuits.
|
102
151
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
Also, more options will make debugging of complex, nested workflows easier.
|
107
|
-
{% endcallout %}
|
152
|
+
> 🌅 In future versions of Trailblazer, our own debugger will take advantage of the explicit, traceable nature of circuits and also integrate with Ruby's exception handling.
|
153
|
+
> Also, more options will make debugging of complex, nested workflows easier.
|
108
154
|
|
109
155
|
## Event
|
110
156
|
|
@@ -114,4 +160,4 @@ Also, more options will make debugging of complex, nested workflows easier.
|
|
114
160
|
|
115
161
|
## Operation
|
116
162
|
|
117
|
-
If you need a higher abstraction of `circuit`, check out Trailblazer's [operation](
|
163
|
+
If you need a higher abstraction of `circuit`, check out Trailblazer's [operation](http://trailblazer.to/gems/operation/2.0/api.html) implemenation which provides a simple Railway-oriented interface to create linear circuits.
|
data/lib/trailblazer/activity.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
require "trailblazer/activity/version"
|
2
|
-
|
3
1
|
require "trailblazer/circuit"
|
4
|
-
require "trailblazer/circuit/trace"
|
5
|
-
require "trailblazer/circuit/present"
|
6
|
-
require "trailblazer/circuit/wrap"
|
7
2
|
|
3
|
+
# TODO: move to separate gem.
|
8
4
|
require "trailblazer/option"
|
9
5
|
require "trailblazer/context"
|
10
6
|
require "trailblazer/container_chain"
|
@@ -12,8 +8,12 @@ require "trailblazer/container_chain"
|
|
12
8
|
module Trailblazer
|
13
9
|
class Activity
|
14
10
|
|
11
|
+
require "trailblazer/activity/version"
|
15
12
|
require "trailblazer/activity/graph"
|
16
13
|
require "trailblazer/activity/nested"
|
14
|
+
require "trailblazer/activity/trace"
|
15
|
+
require "trailblazer/activity/present"
|
16
|
+
require "trailblazer/activity/wrap"
|
17
17
|
|
18
18
|
# Only way to build an Activity.
|
19
19
|
def self.from_wirings(wirings, &block)
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Trailblazer
|
2
|
-
class
|
2
|
+
class Activity
|
3
3
|
# Trace#call will call the activities and trace what steps are called, options passed,
|
4
4
|
# and the order and nesting.
|
5
5
|
#
|
6
|
-
# stack, _ = Trailblazer::
|
7
|
-
# puts Trailblazer::
|
6
|
+
# stack, _ = Trailblazer::Activity::Trace.(activity, activity[:Start], { id: 1 })
|
7
|
+
# puts Trailblazer::Activity::Present.tree(stack) # renders the trail.
|
8
8
|
#
|
9
9
|
# Hooks into the TaskWrap.
|
10
10
|
module Trace
|
@@ -31,8 +31,8 @@ module Trailblazer
|
|
31
31
|
# Default tracing tasks to be plugged into the wrap circuit.
|
32
32
|
def self.wirings
|
33
33
|
[
|
34
|
-
[ :insert_before!, "task_wrap.call_task", node: [ Trace.method(:capture_args), id: "task_wrap.capture_args" ], outgoing: [ Right, {} ], incoming: ->(*) { true } ],
|
35
|
-
[ :insert_before!, "End.default", node: [ Trace.method(:capture_return), id: "task_wrap.capture_return" ], outgoing: [ Right, {} ], incoming: ->(*) { true } ],
|
34
|
+
[ :insert_before!, "task_wrap.call_task", node: [ Trace.method(:capture_args), id: "task_wrap.capture_args" ], outgoing: [ Circuit::Right, {} ], incoming: ->(*) { true } ],
|
35
|
+
[ :insert_before!, "End.default", node: [ Trace.method(:capture_return), id: "task_wrap.capture_return" ], outgoing: [ Circuit::Right, {} ], incoming: ->(*) { true } ],
|
36
36
|
]
|
37
37
|
end
|
38
38
|
|
@@ -1,6 +1,6 @@
|
|
1
|
-
class Trailblazer::
|
1
|
+
class Trailblazer::Activity
|
2
2
|
module Wrap
|
3
|
-
# The runner is passed into
|
3
|
+
# The runner is passed into Activity#call( runner: Runner ) and is called for every task in the circuit.
|
4
4
|
# Its primary job is to actually `call` the task.
|
5
5
|
#
|
6
6
|
# Here, we extend this, and wrap the task `call` into its own pipeline, so we can add external behavior per task.
|
@@ -69,7 +69,7 @@ class Trailblazer::Circuit
|
|
69
69
|
# |-- Trace.capture_return [optional]
|
70
70
|
# |-- End
|
71
71
|
|
72
|
-
# Activity = Trailblazer::
|
72
|
+
# Activity = Trailblazer::Activity::Activity({ id: "task.wrap" }, end: { default: End.new(:default) }) do |act|
|
73
73
|
# {
|
74
74
|
# act[:Start] => { Right => Call }, # see Wrap::call_task
|
75
75
|
# Call => { Right => act[:End] },
|
@@ -79,8 +79,8 @@ class Trailblazer::Circuit
|
|
79
79
|
def self.initial_activity
|
80
80
|
Trailblazer::Activity.from_wirings(
|
81
81
|
[
|
82
|
-
[ :attach!, target: [ End.new(:default), type: :event, id: "End.default" ], edge: [ Right, {} ] ],
|
83
|
-
[ :insert_before!, "End.default", node: [ Call, id: "task_wrap.call_task" ], outgoing: [ Right, {} ], incoming: ->(*) { true } ]
|
82
|
+
[ :attach!, target: [ End.new(:default), type: :event, id: "End.default" ], edge: [ Trailblazer::Circuit::Right, {} ] ],
|
83
|
+
[ :insert_before!, "End.default", node: [ Call, id: "task_wrap.call_task" ], outgoing: [ Trailblazer::Circuit::Right, {} ], incoming: ->(*) { true } ]
|
84
84
|
]
|
85
85
|
)
|
86
86
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trailblazer-activity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hirb
|
@@ -98,12 +98,12 @@ files:
|
|
98
98
|
- lib/trailblazer/activity.rb
|
99
99
|
- lib/trailblazer/activity/graph.rb
|
100
100
|
- lib/trailblazer/activity/nested.rb
|
101
|
+
- lib/trailblazer/activity/present.rb
|
102
|
+
- lib/trailblazer/activity/trace.rb
|
101
103
|
- lib/trailblazer/activity/version.rb
|
104
|
+
- lib/trailblazer/activity/wrap.rb
|
102
105
|
- lib/trailblazer/circuit.rb
|
103
|
-
- lib/trailblazer/circuit/present.rb
|
104
106
|
- lib/trailblazer/circuit/testing.rb
|
105
|
-
- lib/trailblazer/circuit/trace.rb
|
106
|
-
- lib/trailblazer/circuit/wrap.rb
|
107
107
|
- lib/trailblazer/container_chain.rb
|
108
108
|
- lib/trailblazer/context.rb
|
109
109
|
- lib/trailblazer/option.rb
|