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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b34de60db17b5f35bc766ec5d500aeeb7aaf71741084a8f2da94054941f70820
4
- data.tar.gz: 98a53ad41899e0a53b92c5c0ee584ec272c5c86e91812f2683dc94b3640d56d0
3
+ metadata.gz: 215c8d6c36a00683966575960ce1ae019d418308ef9589b5acad9032eebc9b65
4
+ data.tar.gz: e0ada82086f7f9893b5439e4d2dcca9362a0042566a406dcf7d4a83c4a718cd7
5
5
  SHA512:
6
- metadata.gz: 6853892b0d41c33093d1a1bbae4cd64e34d9e5743fb56912426fc3b24ff326ccdab3c8afccd4f624100f2d8e904ae12193ffdfae0527b741422e1c2d7edc13e5
7
- data.tar.gz: 97ea90aef5c24d89151881359b6a6c8b584116d264cb6ed1b2f192a897d3128e7afeb8b72df66a0d3cb37b85522924b055efdbd0f834ae854f85099920346260
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.transition!(order, current_user, to: :parcel__received)
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.transition!(order, current_user, to: :parcel__received) do
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,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- closed_loop (0.1.0)
4
+ closed_loop (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -23,6 +23,7 @@ GEM
23
23
  rspec-support (3.11.0)
24
24
 
25
25
  PLATFORMS
26
+ ruby
26
27
  x86_64-darwin-20
27
28
 
28
29
  DEPENDENCIES
@@ -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
- delegate :transition, :callback, :constraint, to: :@configuration
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 transition!(target, user, to:, &block)
19
- available_transition = available_transitions(target, user).find do |transition|
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
- if available_transition&.available?(target, user)
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 == machine.resolve_role(target, user) &&
10
- target.status.to_sym == from &&
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ClosedLoop
4
- VERSION = "0.1.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/closed_loop.rb CHANGED
@@ -5,4 +5,4 @@ require_relative "closed_loop/machine"
5
5
 
6
6
  module ClosedLoop
7
7
  class Error < StandardError; end
8
- end
8
+ end
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.1.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-07-12 00:00:00.000000000 Z
11
+ date: 2022-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler