estate 0.1.1 → 0.2.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: 0777265b6deae44f96eb4fbc8f150bb73b1f2d98849107b9750eeb0ac025ca49
4
- data.tar.gz: 916c2a835c05ed793d87e25883cb6999ebf87687c260f39eba90e994cf0aa228
3
+ metadata.gz: 4b3da0543146b0a82972f87969bc2278b1deb7ab600965370caf143bd8b342d7
4
+ data.tar.gz: e87a26965c47b976746059431196048fb2b03cb829475e5e1eabf48b80054694
5
5
  SHA512:
6
- metadata.gz: 37e595f2f4451386df31a2b563af980037c90f91786760c9c88f9090418ffd3b640473bc5c2d16a1826c3646302d2041b151a0807d106e1dbc6c85dc126bbfbd
7
- data.tar.gz: f939381283ace662a7d97169cc7e0bf26ad8d05a78eb40585f456e84768bda4474c1ae0501d5f7d232eb401d577341d2ce09f4fa3b02137adca8019e87c65114
6
+ metadata.gz: bd904b3f3dbcf36895fb76c3c76ce05c7cb4af45ab30150fb8d9becd2120c9a7add11ada7051396e3f5cfc7093702221b7f6e77de994e04e347f1018802ce0f3
7
+ data.tar.gz: c69b6df5691fcc4104c28e9ce5eb23ad294c54002a8ff32c498750074c7c6bded4bbe7cb0707d69f3abd23ab4f47e67b59ca25571784c008e63c41135620e76e
@@ -2,16 +2,6 @@
2
2
 
3
3
  module Estate
4
4
  module Configuration
5
- class << self
6
- def init_config(column_name, allow_empty_initial_state, raise_on_error)
7
- @column_name = column_name
8
- @allow_empty_initial_state = allow_empty_initial_state
9
- @raise_on_error = raise_on_error
10
- end
11
-
12
- attr_reader :column_name, :allow_empty_initial_state, :raise_on_error
13
- end
14
-
15
5
  module Defaults
16
6
  COLUMN_NAME = :state
17
7
  ALLOW_EMPTY_INITIAL_STATE = false
data/lib/estate/estate.rb CHANGED
@@ -5,7 +5,6 @@ module Estate
5
5
  base.extend Estate::ClassMethods
6
6
 
7
7
  Estate::Requirements.check_requirements(base)
8
- Estate::StateMachine.create_store
9
8
  Estate::Setup.call(base)
10
9
  end
11
10
 
@@ -13,18 +12,18 @@ module Estate
13
12
  def estate(column: Estate::Configuration::Defaults::COLUMN_NAME,
14
13
  empty_initial_state: Estate::Configuration::Defaults::ALLOW_EMPTY_INITIAL_STATE,
15
14
  raise_on_error: Estate::Configuration::Defaults::RAISE_ON_ERROR)
16
- Estate::Configuration.init_config(column, empty_initial_state, raise_on_error)
15
+ Estate::StateMachine.init(name, column, empty_initial_state, raise_on_error)
17
16
 
18
17
  yield if block_given?
19
18
  end
20
19
 
21
- def state(name = nil)
22
- raise(StandardError, 'state must be a Symbol or a String') unless Estate::StateMachine.argument_valid?(name)
20
+ def state(state_name = nil)
21
+ raise(StandardError, 'state must be a Symbol or a String') unless Estate::StateMachine.argument_valid?(state_name)
23
22
 
24
- if Estate::StateMachine.state_exists?(name)
25
- raise(StandardError, "state `:#{name}` is already defined")
23
+ if Estate::StateMachine.state_exists?(name, state_name)
24
+ raise(StandardError, "state `:#{state_name}` is already defined")
26
25
  else
27
- Estate::StateMachine.register_state(name)
26
+ Estate::StateMachine.register_state(name, state_name)
28
27
  end
29
28
  end
30
29
 
@@ -35,15 +34,15 @@ module Estate
35
34
 
36
35
  raise(StandardError, 'argument `to` must be a Symbol or a String') unless Estate::StateMachine.argument_valid?(to)
37
36
 
38
- raise(StandardError, "state `#{from}` is not defined") unless Estate::StateMachine.state_exists?(from)
37
+ raise(StandardError, "state `#{from}` is not defined") unless Estate::StateMachine.state_exists?(name, from)
39
38
 
40
- raise(StandardError, "state `#{to}` is not defined") unless Estate::StateMachine.state_exists?(to)
39
+ raise(StandardError, "state `#{to}` is not defined") unless Estate::StateMachine.state_exists?(name, to)
41
40
 
42
- if Estate::StateMachine.transition_exists?(from, to)
41
+ if Estate::StateMachine.transition_exists?(name, from, to)
43
42
  raise(StandardError, "`transition from: :#{from}, to: :#{to}` already defined")
44
43
  end
45
44
 
46
- Estate::StateMachine.register_transition(from, to)
45
+ Estate::StateMachine.register_transition(name, from, to)
47
46
  end
48
47
  end
49
48
  end
@@ -11,7 +11,7 @@ module Estate
11
11
  module_function
12
12
 
13
13
  def add_error(instance, message, attribute: :base)
14
- if Estate::Configuration.raise_on_error
14
+ if config_for(instance)[:raise_on_error]
15
15
  exception_message = attribute == :base ? message : "#{attribute}: #{message}"
16
16
  raise(StandardError, exception_message)
17
17
  else
@@ -20,8 +20,8 @@ module Estate
20
20
  end
21
21
 
22
22
  def get_states(instance)
23
- from_state = instance.public_send("#{Estate::Configuration.column_name}_was")
24
- to_state = instance.public_send(Estate::Configuration.column_name)
23
+ from_state = instance.public_send("#{config_for(instance)[:column_name]}_was")
24
+ to_state = instance.public_send(config_for(instance)[:column_name])
25
25
  [from_state, to_state]
26
26
  end
27
27
  end
@@ -4,22 +4,29 @@ module Estate
4
4
  module Logic
5
5
  module CommonLogic
6
6
  def validate_state_changes(instance, from_state, to_state)
7
+ state_machine_name = instance.class.name
8
+
7
9
  if from_state == to_state
8
- if from_state.nil? && !Estate::Configuration.allow_empty_initial_state
9
- add_error(instance, "empty `#{Estate::Configuration.column_name}` is not allowed")
10
+ if from_state.nil? && !config_for(instance)[:empty_initial_state]
11
+ add_error(instance, "empty `#{config_for(instance)[:column_name]}` is not allowed")
10
12
  end
11
13
  elsif to_state.nil?
12
14
  add_error(instance, 'transition to empty state is not allowed')
13
- elsif !Estate::StateMachine.state_exists?(to_state)
15
+ elsif !Estate::StateMachine.state_exists?(state_machine_name, to_state)
14
16
  add_error(instance, "state `#{to_state}` is not defined")
15
- elsif !transition_allowed?(from_state, to_state)
17
+ elsif !transition_allowed?(state_machine_name, from_state, to_state)
16
18
  add_error(instance, "transition from `#{from_state}` to `#{to_state}` is not allowed",
17
- attribute: Estate::Configuration.column_name)
19
+ attribute: config_for(instance)[:column_name])
18
20
  end
19
21
  end
20
22
 
21
- def transition_allowed?(from_state, to_state)
22
- from_state.nil? || Estate::StateMachine.transition_exists?(from_state, to_state)
23
+ def transition_allowed?(state_machine_name, from_state, to_state)
24
+ from_state.nil? || Estate::StateMachine.transition_exists?(state_machine_name, from_state, to_state)
25
+ end
26
+
27
+ def config_for(instance)
28
+ state_machine_name = instance.class.name
29
+ Estate::StateMachine.state_machines[state_machine_name][:config]
23
30
  end
24
31
  end
25
32
  end
@@ -16,8 +16,8 @@ module Estate
16
16
  end
17
17
 
18
18
  def get_states(instance)
19
- from_state, = instance.column_change(Estate::Configuration.column_name)
20
- to_state = instance.values[Estate::Configuration.column_name]
19
+ from_state, = instance.column_change(config_for(instance)[:column_name])
20
+ to_state = instance.values[config_for(instance)[:column_name]]
21
21
  [from_state, to_state]
22
22
  end
23
23
  end
@@ -3,34 +3,42 @@
3
3
  module Estate
4
4
  class StateMachine
5
5
  class << self
6
- def create_store
7
- @states = {}
8
- @transitions = {}
6
+ def init(state_machine_name, column_name, empty_initial_state, raise_on_error)
7
+ @state_machines ||= {}
8
+ @state_machines[state_machine_name] = {
9
+ config: {
10
+ column_name: column_name,
11
+ empty_initial_state: empty_initial_state, # TODO: allow_empty_initial_state ?
12
+ raise_on_error: raise_on_error
13
+ },
14
+ states: {},
15
+ transitions: {}
16
+ }
9
17
  end
10
18
 
11
- def state_exists?(state)
12
- states.key?(state.to_sym)
19
+ def state_exists?(state_machine_name, state_name)
20
+ state_machines[state_machine_name][:states].key?(state_name.to_sym)
13
21
  end
14
22
 
15
- def register_state(state)
16
- states[state.to_sym] = nil
23
+ def register_state(state_machine_name, state_name)
24
+ state_machines[state_machine_name][:states][state_name.to_sym] = nil
17
25
  end
18
26
 
19
- def transition_exists?(from_state, to_state)
27
+ def transition_exists?(state_machine_name, from_state, to_state)
20
28
  transition_key = { from: from_state.to_sym, to: to_state.to_sym }
21
- transitions.key?(transition_key)
29
+ state_machines[state_machine_name][:transitions].key?(transition_key)
22
30
  end
23
31
 
24
- def register_transition(from_state, to_state)
32
+ def register_transition(state_machine_name, from_state, to_state)
25
33
  transition_key = { from: from_state.to_sym, to: to_state.to_sym }
26
- transitions[transition_key] = nil
34
+ state_machines[state_machine_name][:transitions][transition_key] = nil
27
35
  end
28
36
 
29
37
  def argument_valid?(argument)
30
38
  argument.is_a?(Symbol) || argument.is_a?(String)
31
39
  end
32
40
 
33
- attr_reader :states, :transitions
41
+ attr_reader :state_machines
34
42
  end
35
43
  end
36
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Estate
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: estate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Korepanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-29 00:00:00.000000000 Z
11
+ date: 2024-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop