state_machina 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03d12ffeaf652cc4da21dfce6218b807c15234179973298ae52b5201f0439ae6
4
- data.tar.gz: 06bddb26f70659df01bbc8a9e23cf586e0f616cc26b7ec1c267996a64342b7b2
3
+ metadata.gz: 72088dda2911712eec95b2a4915a9c62375cf43cfdde49338d58c32d448e108a
4
+ data.tar.gz: de86ad656d1abb2d6fd06d32dfc49e8030a2dfde4413194adafecfe679c086c3
5
5
  SHA512:
6
- metadata.gz: 771e60029f66d6f0b8ea70c599335356ae9944e05d4148e7b8eccab34c18e5e506c3ee9b6ca69546789830abc0b52482ad26788e5414d66cec3b97e051c33380
7
- data.tar.gz: 9302926d8356f8858b196a340665e455734d13897c464b4d1cd8a9363aad05a08eaacd3cd378a504b24054cd8c615bcb92a52921a0d5a1ca722563625ab92ec3
6
+ metadata.gz: a9f9d175b449b71693ccb0c879d7022831dbdc90aa6e5efd38d16961090bc235ba28e6952b9f8e4989ec5b1a5cf5ac00994bcdad79d01200fa48a13f1486ab28
7
+ data.tar.gz: 4fee723a2a9372cffd177c492c901b0e40aeb9d04025f2514e72dfb04b64929eaa8c572b10fb0ba4f3b1a8ed44d61adc7c90385f38fbec6c83e00f3cb1aded17
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- state_machina (0.1.0)
4
+ state_machina (0.1.1)
5
5
  concurrent-ruby (< 2)
6
6
 
7
7
  GEM
@@ -9,7 +9,7 @@ GEM
9
9
  specs:
10
10
  ast (2.4.2)
11
11
  coderay (1.1.3)
12
- concurrent-ruby (1.1.9)
12
+ concurrent-ruby (1.2.2)
13
13
  diff-lcs (1.5.0)
14
14
  method_source (1.0.0)
15
15
  parallel (1.21.0)
@@ -0,0 +1,18 @@
1
+ module StateMachina
2
+ module ExtendedIntegration
3
+ module ClassMethods
4
+ # Extends a machine on a model where the machine is already defined
5
+ def extend_machine(klass, machine_name = 'default')
6
+ model_name = StateMachina::Util.normalized_klass_to_name(klass)
7
+
8
+ yield(StateMachina::Registry.find_machine!(model_name, machine_name))
9
+ end
10
+
11
+ alias_method :extend_state_machine, :extend_machine
12
+ end
13
+
14
+ def self.included(model)
15
+ model.extend(StateMachina::ExtendedIntegration::ClassMethods)
16
+ end
17
+ end
18
+ end
@@ -1,9 +1,49 @@
1
1
  module StateMachina
2
2
  module Integration
3
+ module ClassMethods
4
+ # Defines a new machine on a model
5
+ def define_machine(machine_name = 'default', column_name: :state, metadata: {})
6
+ model_name = StateMachina::Util.normalized_klass_to_name(self)
7
+ machine = StateMachina::Machine.new(model_name, machine_name, column_name: column_name, metadata: metadata)
8
+
9
+ yield(StateMachina::Registry.register_machine(machine))
10
+ end
11
+
12
+ alias_method :define_state_machine, :define_machine
13
+
14
+ # Extends a machine on a model where the machine is already defined
15
+ def extend_machine(klass, machine_name = 'default')
16
+ model_name = StateMachina::Util.normalized_klass_to_name(klass)
17
+
18
+ yield(StateMachina::Registry.find_machine!(model_name, machine_name))
19
+ end
20
+
21
+ alias_method :extend_state_machine, :extend_machine
22
+
23
+ # Returns the defined machine on a model
24
+ def machine(machine_name = 'default')
25
+ model_name = StateMachina::Util.normalized_klass_to_name(self)
26
+
27
+ StateMachina::Registry.find_machine!(model_name, machine_name)
28
+ end
29
+
30
+ alias_method :state_machine, :machine
31
+
32
+ # Returns the defined machines on a model
33
+ def machines
34
+ model_name = StateMachina::Util.normalized_klass_to_name(self)
35
+
36
+ StateMachina::Registry.find_machines(model_name)
37
+ end
38
+
39
+ alias_method :state_machines, :machines
40
+ end
41
+
3
42
  def self.included(model)
4
- model.extend StateMachina::Definitions
43
+ model.extend(StateMachina::Integration::ClassMethods)
5
44
  end
6
45
 
46
+ # Returns the defined machine on a model instance
7
47
  def machine(machine_name = 'default')
8
48
  model_name = StateMachina::Util.normalized_klass_to_name(self.class)
9
49
 
@@ -14,6 +54,7 @@ module StateMachina
14
54
 
15
55
  alias_method :state_machine, :machine
16
56
 
57
+ # Returns the defined machines on a model instance
17
58
  def machines
18
59
  model_name = StateMachina::Util.normalized_klass_to_name(self.class)
19
60
 
@@ -53,7 +53,11 @@ module StateMachina
53
53
  end
54
54
 
55
55
  def update_state(new_state)
56
- model.public_send("#{column_name}=", new_state)
56
+ if model.respond_to?(:update)
57
+ model.update(column_name => new_state)
58
+ else
59
+ model.public_send("#{column_name}=", new_state)
60
+ end
57
61
  end
58
62
 
59
63
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StateMachina
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/state_machina.rb CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  require "concurrent"
4
4
 
5
- require_relative "state_machina/definitions"
6
5
  require_relative "state_machina/event"
7
6
  require_relative "state_machina/events_collection"
7
+ require_relative "state_machina/extended_integration"
8
8
  require_relative "state_machina/integration"
9
9
  require_relative "state_machina/machine"
10
10
  require_relative "state_machina/registry"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: state_machina
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan van der Pas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-23 00:00:00.000000000 Z
11
+ date: 2023-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -56,9 +56,9 @@ files:
56
56
  - README.md
57
57
  - Rakefile
58
58
  - lib/state_machina.rb
59
- - lib/state_machina/definitions.rb
60
59
  - lib/state_machina/event.rb
61
60
  - lib/state_machina/events_collection.rb
61
+ - lib/state_machina/extended_integration.rb
62
62
  - lib/state_machina/integration.rb
63
63
  - lib/state_machina/machine.rb
64
64
  - lib/state_machina/registry.rb
@@ -1,20 +0,0 @@
1
- module StateMachina
2
- module Definitions
3
- def machine(machine_name = 'default', column_name: :state, metadata: {})
4
- model_name = StateMachina::Util.normalized_klass_to_name(self)
5
- machine = StateMachina::Machine.new(model_name, machine_name, column_name: column_name, metadata: metadata)
6
-
7
- yield(StateMachina::Registry.register_machine(machine))
8
- end
9
-
10
- alias_method :state_machine, :machine
11
-
12
- def extend_machine(klass, machine_name = 'default')
13
- model_name = StateMachina::Util.normalized_klass_to_name(klass)
14
-
15
- yield(StateMachina::Registry.find_machine!(model_name, machine_name))
16
- end
17
-
18
- alias_method :extend_state_machine, :extend_machine
19
- end
20
- end