closed_loop 0.1.0 → 0.4.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 +4 -4
- data/Example.md +2 -2
- data/Gemfile.lock +2 -1
- data/lib/closed_loop/machine/instance.rb +19 -8
- data/lib/closed_loop/machine/transition.rb +22 -2
- data/lib/closed_loop/version.rb +1 -1
- data/lib/closed_loop.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: 215c8d6c36a00683966575960ce1ae019d418308ef9589b5acad9032eebc9b65
|
4
|
+
data.tar.gz: e0ada82086f7f9893b5439e4d2dcca9362a0042566a406dcf7d4a83c4a718cd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d128e66f02f90e054623afe177a02b42c021d63d61f78099f62e82901f9d3d9ba0eac8daedd4e632ce5646bf9eb1980ba0ffbf94520a07d527e8c7ee279bfb4e
|
7
|
+
data.tar.gz: bb0e34235ed7b41e1e4ecf650385fc93921b0a3100aca253845db35c12983d66bb196b485263630e817c15d1cad53158fc23a239aebc3e38e500c391e205fe91
|
data/Example.md
CHANGED
@@ -77,11 +77,11 @@ end
|
|
77
77
|
```Ruby
|
78
78
|
# In controller / service / interactor / background worker
|
79
79
|
|
80
|
-
OrderMachine.insantce.
|
80
|
+
OrderMachine.insantce.transit!(order, current_user, to: :parcel__received)
|
81
81
|
|
82
82
|
# invoke extra methods in the the same transition & ActiveRecord transaction:
|
83
83
|
|
84
|
-
OrderMachine.insantce.
|
84
|
+
OrderMachine.insantce.transit!(order, current_user, to: :parcel__received) do
|
85
85
|
order.items.each { |item| '#...' }
|
86
86
|
end
|
87
87
|
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,13 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
1
3
|
module ClosedLoop
|
2
4
|
module Machine
|
3
5
|
class Instance
|
6
|
+
extend Forwardable
|
7
|
+
|
4
8
|
attr_reader :configuration
|
5
9
|
|
6
|
-
|
10
|
+
def_delegators :@configuration, :transition, :callback, :constraint
|
7
11
|
|
8
12
|
def initialize
|
9
13
|
@configuration = ClosedLoop::Machine::Configuration.new(self)
|
@@ -15,16 +19,23 @@ module ClosedLoop
|
|
15
19
|
configuration.transitions.select { |transition| transition.available?(target, user) }
|
16
20
|
end
|
17
21
|
|
18
|
-
def
|
19
|
-
|
20
|
-
transition.from == target.status.to_sym && transition.to == to.to_sym
|
21
|
-
end
|
22
|
+
def transit!(target, user, to:, &block)
|
23
|
+
transition = find_transition(target, user, to:)
|
22
24
|
|
23
|
-
|
24
|
-
available_transition.perform!(target, user, &block)
|
25
|
-
else
|
25
|
+
unless transition
|
26
26
|
raise("Transition #{self.class} #{target.status}->#{to} for #{target.id} by #{user} not available!")
|
27
27
|
end
|
28
|
+
|
29
|
+
transition.perform!(target, user, &block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def transit(target, user, to:, attributes: {}, &block)
|
33
|
+
transition = find_transition(target, user, to:)
|
34
|
+
transition&.perform(target, user, attributes, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def find_transition(target, user, to:)
|
38
|
+
available_transitions(target, user).find { |transition| transition.to == to.to_sym }
|
28
39
|
end
|
29
40
|
|
30
41
|
def resolve_role(*args)
|
@@ -6,8 +6,8 @@ module ClosedLoop
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def available?(target, user)
|
9
|
-
role
|
10
|
-
target.status.to_sym
|
9
|
+
Array(role).include?(machine.resolve_role(target, user)) &&
|
10
|
+
Array(from).include?(target.status.to_sym) &&
|
11
11
|
none_constraints?(target, user)
|
12
12
|
end
|
13
13
|
|
@@ -37,6 +37,26 @@ module ClosedLoop
|
|
37
37
|
|
38
38
|
@track_times_used = times_used + 1
|
39
39
|
end
|
40
|
+
|
41
|
+
def perform(target, user, attributes)
|
42
|
+
target.class.transaction do
|
43
|
+
target.assign_attributes(attributes)
|
44
|
+
target.status = to
|
45
|
+
target.last_transition_at = Time.current if target.respond_to?(:last_transition_at)
|
46
|
+
|
47
|
+
if target.save
|
48
|
+
proc&.call(target, user, transition: self)
|
49
|
+
|
50
|
+
machine.configuration.select_callbacks_for(self).each do |callback|
|
51
|
+
callback.perform!(target, user, transition: self)
|
52
|
+
end
|
53
|
+
|
54
|
+
yield(target, user, transition: self) if block_given?
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
@track_times_used = times_used + 1
|
59
|
+
end
|
40
60
|
end
|
41
61
|
end
|
42
62
|
end
|
data/lib/closed_loop/version.rb
CHANGED
data/lib/closed_loop.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: closed_loop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vilius Luneckas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|