estate 0.0.1 → 0.1.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: e432065bf5d5370925a9ba65ca110ebad8c4c3810feaa2302c281d43b4d93426
4
- data.tar.gz: cd71905ea5af259bc6b970cd91bb215d16934bb76892815439876ca91814d199
3
+ metadata.gz: a5e27471f690e289d113e8e69a6c72ad1c063a20b57bdead1f97040b71cb101f
4
+ data.tar.gz: 233b56c93ade0cc71d91d721bd58f207bc17bc320b9e2165d31ad2a4700786d7
5
5
  SHA512:
6
- metadata.gz: c68e50c0e42ebbf1700d9ee93cbcdfd24d1e33a289eb674f1346148035fb92d9bedb492331ddc611f4c987c0699895fa28d74274668c8ea0930f199fe3e377a9
7
- data.tar.gz: 60fd96c4b66633ee872546c82d087f433532e32ec0cbe89a3df56edfd636cfcc93e596f4f2010f9115281907d96727b3439fa762f72c71a118b92cbbbfd68e7c
6
+ metadata.gz: c1330c6a3512171bdb6deaa89612fa4adf4d6aad6ef621e97aa456c0e792e48f21b1d7cac9ef47fe36a7a8b1806c6148da9b973200e1ff5f27e5434deac43c11
7
+ data.tar.gz: 1452e5b9408d8cfd658ed983d11f1f8a0300e533400875285af65c84ee7a451edf6f29be6df4f779e991ce47a25fd6e230404b8949d37a15a1353414ef1be521
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2023 Igor Korepanov
1
+ Copyright (c) 2023-2024 Igor Korepanov
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -0,0 +1,135 @@
1
+ # Estate Gem
2
+
3
+ Estate is a Ruby gem designed to simplify state management in both ActiveRecord and Sequel models. The primary focus of this gem is to provide a straightforward way to define states and transitions using a clean syntax.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile and run `bundle install`:
8
+
9
+ ```ruby
10
+ gem 'estate'
11
+ ```
12
+
13
+ Or install it manually using:
14
+
15
+ ```bash
16
+ gem install estate
17
+ ```
18
+
19
+ ## Usage
20
+ ### ActiveRecord
21
+ To use the Estate gem with ActiveRecord, include it in your model and define your states and transitions inside a block using the `estate` method. Here's a simple example:
22
+
23
+ ```ruby
24
+ class MyModel < ApplicationRecord
25
+ include Estate
26
+
27
+ estate do
28
+ state :state_1
29
+ state :state_2
30
+ state :state_3
31
+
32
+ transition from: :state_1, to: :state_2
33
+ transition from: :state_2, to: :state_3
34
+ transition from: :state_3, to: :state_1
35
+ end
36
+ end
37
+ ```
38
+
39
+ And then
40
+
41
+ ```ruby
42
+ model = MyModel.create(state: :state_1)
43
+ model.update(state: :state_2) # you don't need to call any extra code to change the state; treat it like a normal field
44
+ ```
45
+
46
+ The default field for storing the state is named "state". You can customize this name by providing options to the estate method:
47
+
48
+ ```ruby
49
+ class MyModel < ApplicationRecord
50
+ include Estate
51
+
52
+ estate column: :custom_state_field do
53
+ # ...
54
+ end
55
+ end
56
+ ```
57
+
58
+ You can also use the `empty_initial_state: true` option to enable the creation of a model with a `nil` initial state:
59
+
60
+ ```ruby
61
+ class MyModel < ApplicationRecord
62
+ include Estate
63
+
64
+ estate empty_initial_state: true do
65
+ # ...
66
+ end
67
+ end
68
+ ```
69
+
70
+ The `estate` method now supports a `raise_on_error` option. When set to `true`, the gem will raise a specific exception instead of the standard ActiveRecord validation error upon a validation failure.
71
+
72
+ ```ruby
73
+ class MyModel < ApplicationRecord
74
+ include Estate
75
+
76
+ estate raise_on_error: true do
77
+ # ...
78
+ end
79
+ end
80
+ ```
81
+
82
+ ### Sequel
83
+ To use the Estate gem with Sequel, include it in your model, and ensure you have the `plugin: dirty` enabled for validation to work correctly. The `raise_on_error` option is not needed with Sequel, as exceptions are always raised on validation errors.
84
+
85
+ ```ruby
86
+ class MySequelModel < Sequel::Model
87
+ include Estate
88
+
89
+ plugin :dirty # Ensure the dirty plugin is enabled for validation to work
90
+
91
+ estate do
92
+ state :state_1
93
+ state :state_2
94
+ state :state_3
95
+
96
+ transition from: :state_1, to: :state_2
97
+ transition from: :state_2, to: :state_3
98
+ transition from: :state_3, to: :state_1
99
+ end
100
+ end
101
+ ```
102
+
103
+ ## Migration Example for ActiveRecord
104
+
105
+ ```bash
106
+ bundle exec rails generate migration AddStateToMyModels state:string
107
+ ```
108
+
109
+ This will generate a migration file. Open the migration file and modify it to suit your needs, for example:
110
+
111
+ ```ruby
112
+ class AddStateToMyModels < ActiveRecord::Migration[7.0]
113
+ def change
114
+ add_column :my_models, :state, :string
115
+ end
116
+ end
117
+ ```
118
+
119
+ Run the migration:
120
+
121
+ ```bash
122
+ bundle exec rails db:migrate
123
+ ```
124
+
125
+ ## License
126
+
127
+ Copyright (c) 2023-2024 Igor Korepanov
128
+
129
+ MIT License
130
+
131
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
132
+
133
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
134
+
135
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -3,17 +3,19 @@
3
3
  module Estate
4
4
  module Configuration
5
5
  class << self
6
- def init_config(column_name:, allow_empty_initial_state:)
6
+ def init_config(column_name:, allow_empty_initial_state:, raise_on_error:)
7
7
  @column_name = column_name
8
8
  @allow_empty_initial_state = allow_empty_initial_state
9
+ @raise_on_error = raise_on_error
9
10
  end
10
11
 
11
- attr_reader :column_name, :allow_empty_initial_state
12
+ attr_reader :column_name, :allow_empty_initial_state, :raise_on_error
12
13
  end
13
14
 
14
15
  module Defaults
15
16
  COLUMN_NAME = :state
16
17
  ALLOW_EMPTY_INITIAL_STATE = false
18
+ RAISE_ON_ERROR = false
17
19
  end
18
20
  end
19
- end
21
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Estate
4
+ module Core
5
+ module_function
6
+
7
+ def setup(base)
8
+ if 'ActiveRecord::Base'.in? base.ancestors.map(&:to_s)
9
+ require File.join(File.dirname(__FILE__), 'db', 'active_record')
10
+ Estate::Db::ActiveRecord.setup_callbacks(base)
11
+ else
12
+ require File.join(File.dirname(__FILE__), 'db', 'sequel')
13
+ Estate::Db::Sequel.setup_callbacks(base)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Estate
4
+ module Db
5
+ module ActiveRecord
6
+ module_function
7
+
8
+ def setup_callbacks(base)
9
+ base.class_eval do
10
+ public_send(:before_validation) do
11
+ from_state = public_send("#{Estate::Configuration.column_name}_was")
12
+ to_state = public_send(Estate::Configuration.column_name)
13
+ Estate::Db::ActiveRecord.validate_state_changes(self, from_state, to_state)
14
+ end
15
+ end
16
+ end
17
+
18
+ def validate_state_changes(instance, from_state, to_state)
19
+ if from_state == to_state
20
+ if from_state.nil? && !Estate::Configuration.allow_empty_initial_state
21
+ add_error(instance: instance, message: "empty `#{Estate::Configuration.column_name}` is not allowed")
22
+ end
23
+ elsif to_state.nil?
24
+ add_error(instance: instance, message: 'transition to empty state is not allowed')
25
+ elsif !Estate::StateMachine.state_exists?(to_state)
26
+ add_error(instance: instance, message: "state `#{to_state}` is not defined")
27
+ elsif !transition_allowed?(from_state: from_state, to_state: to_state)
28
+ add_error(instance: instance, message: "transition from `#{from_state}` to `#{to_state}` is not allowed",
29
+ attribute: Estate::Configuration.column_name)
30
+ end
31
+ end
32
+
33
+ def add_error(instance:, message:, attribute: :base)
34
+ if Estate::Configuration.raise_on_error
35
+ exception_message = attribute == :base ? message : "#{attribute}: #{message}"
36
+ raise(StandardError, exception_message)
37
+ else
38
+ instance.errors.add(attribute, message) unless instance.errors[attribute].include?(message)
39
+ end
40
+ end
41
+
42
+ def transition_allowed?(from_state:, to_state:)
43
+ from_state.nil? || Estate::StateMachine.transition_exists?(from: from_state, to: to_state)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Estate
4
+ module Db
5
+ module Sequel
6
+ module_function
7
+
8
+ def setup_callbacks(base)
9
+ base.class_eval do
10
+ def validate
11
+ super
12
+
13
+ to_state = values[Estate::Configuration.column_name]
14
+ from_state, = column_change(Estate::Configuration.column_name)
15
+ Estate::Db::Sequel.validate_state_changes(self, from_state, to_state)
16
+ end
17
+ end
18
+ end
19
+
20
+ def validate_state_changes(instance, from_state, to_state)
21
+ if from_state == to_state
22
+ if from_state.nil? && !Estate::Configuration.allow_empty_initial_state
23
+ add_error(instance: instance, message: "empty `#{Estate::Configuration.column_name}` is not allowed")
24
+ end
25
+ elsif to_state.nil?
26
+ add_error(instance: instance, message: 'transition to empty state is not allowed')
27
+ elsif !Estate::StateMachine.state_exists?(to_state)
28
+ add_error(instance: instance, message: "state `#{to_state}` is not defined")
29
+ elsif !transition_allowed?(from_state: from_state, to_state: to_state)
30
+ add_error(instance: instance, message: "transition from `#{from_state}` to `#{to_state}` is not allowed",
31
+ attribute: Estate::Configuration.column_name)
32
+ end
33
+ end
34
+
35
+ # TODO: remove base
36
+ def add_error(instance:, message:, attribute: :base)
37
+ instance.errors.add(attribute, message)
38
+ end
39
+
40
+ def transition_allowed?(from_state:, to_state:)
41
+ from_state.nil? || Estate::StateMachine.transition_exists?(from: from_state, to: to_state)
42
+ end
43
+ end
44
+ end
45
+ end
data/lib/estate/estate.rb CHANGED
@@ -5,17 +5,16 @@ module Estate
5
5
  base.extend Estate::ClassMethods
6
6
 
7
7
  Estate::Requirements.check_requirements(base)
8
- Estate::ActiveRecord.setup_callbacks(base)
9
8
  Estate::StateMachine.create_store
10
-
11
- # TODO: make sure inheritance (aka subclassing) works with AASM
12
- super
9
+ Estate::Core.setup(base)
13
10
  end
14
11
 
15
12
  module ClassMethods
16
13
  def estate(column: Estate::Configuration::Defaults::COLUMN_NAME,
17
- empty_initial_state: Estate::Configuration::Defaults::ALLOW_EMPTY_INITIAL_STATE)
18
- Estate::Configuration.init_config(column_name: column, allow_empty_initial_state: empty_initial_state)
14
+ empty_initial_state: Estate::Configuration::Defaults::ALLOW_EMPTY_INITIAL_STATE,
15
+ raise_on_error: Estate::Configuration::Defaults::RAISE_ON_ERROR)
16
+ Estate::Configuration.init_config(column_name: column, allow_empty_initial_state: empty_initial_state,
17
+ raise_on_error: raise_on_error)
19
18
 
20
19
  yield if block_given?
21
20
  end
@@ -29,12 +28,10 @@ module Estate
29
28
  end
30
29
 
31
30
  def transition(from:, to:)
32
- unless Estate::StateMachine.state_exists?(from)
33
- raise(StandardError, "state `#{from}` is not defined")
34
- end
35
- unless Estate::StateMachine.state_exists?(to)
36
- raise(StandardError, "state `#{to}` is not defined")
37
- end
31
+ raise(StandardError, "state `#{from}` is not defined") unless Estate::StateMachine.state_exists?(from)
32
+
33
+ raise(StandardError, "state `#{to}` is not defined") unless Estate::StateMachine.state_exists?(to)
34
+
38
35
  if Estate::StateMachine.transition_exists?(from: from, to: to)
39
36
  raise(StandardError, "`transition from: :#{from}, to: :#{to}` already defined")
40
37
  end
@@ -3,13 +3,13 @@
3
3
  module Estate
4
4
  module Requirements
5
5
  def check_requirements(base)
6
- ancestors = base.ancestors.map {|klass| klass.to_s}
6
+ ancestors = base.ancestors.map(&:to_s)
7
7
 
8
- unless ancestors.include?("ActiveRecord::Base")
9
- raise(StandardError, "Estate requires ActiveRecord")
8
+ unless 'Sequel::Model'.in?(ancestors) || 'ActiveRecord::Base'.in?(ancestors)
9
+ raise(StandardError, 'Estate requires ActiveRecord or Sequel')
10
10
  end
11
11
  end
12
12
 
13
13
  module_function :check_requirements
14
14
  end
15
- end
15
+ end
@@ -24,7 +24,6 @@ module Estate
24
24
  end
25
25
 
26
26
  def transition_exists?(from:, to:)
27
- # TODO: validate from and to
28
27
  transition_key = { from: from.to_sym, to: to.to_sym }
29
28
  transitions.key?(transition_key)
30
29
  end
@@ -38,4 +37,4 @@ module Estate
38
37
  attr_reader :states, :transitions
39
38
  end
40
39
  end
41
- end
40
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Estate
4
- VERSION = '0.0.1'
5
- end
4
+ VERSION = '0.1.0'
5
+ end
data/lib/estate.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'estate/version'
4
- require 'estate/requirements'
5
3
  require 'estate/configuration'
6
- require 'estate/state_machine'
7
- require 'estate/active_record'
4
+ require 'estate/core'
8
5
  require 'estate/estate'
6
+ require 'estate/requirements'
7
+ require 'estate/state_machine'
8
+ require 'estate/version'
metadata CHANGED
@@ -1,17 +1,88 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: estate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.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-05 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Estate test gem
14
- email: email@example.com
11
+ date: 2024-01-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.59.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.59.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop-performance
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.20.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.20.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.26.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.26.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.12.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.12.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.22.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.22.0
83
+ description: Estate is a Ruby gem designed to simplify state management in ActiveRecord
84
+ models
85
+ email: noemail@example.com
15
86
  executables: []
16
87
  extensions: []
17
88
  extra_rdoc_files: []
@@ -19,13 +90,15 @@ files:
19
90
  - LICENSE.txt
20
91
  - README.md
21
92
  - lib/estate.rb
22
- - lib/estate/active_record.rb
23
93
  - lib/estate/configuration.rb
94
+ - lib/estate/core.rb
95
+ - lib/estate/db/active_record.rb
96
+ - lib/estate/db/sequel.rb
24
97
  - lib/estate/estate.rb
25
98
  - lib/estate/requirements.rb
26
99
  - lib/estate/state_machine.rb
27
100
  - lib/estate/version.rb
28
- homepage: https://rubygems.org/gems/estate
101
+ homepage: https://github.com/igorkorepanov/estate
29
102
  licenses:
30
103
  - MIT
31
104
  metadata: {}
@@ -37,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
110
  requirements:
38
111
  - - ">="
39
112
  - !ruby/object:Gem::Version
40
- version: '0'
113
+ version: '2.7'
41
114
  required_rubygems_version: !ruby/object:Gem::Requirement
42
115
  requirements:
43
116
  - - ">="
@@ -47,5 +120,5 @@ requirements: []
47
120
  rubygems_version: 3.2.22
48
121
  signing_key:
49
122
  specification_version: 4
50
- summary: Estate
123
+ summary: State machine for Rails models
51
124
  test_files: []
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Estate
4
- module ActiveRecord
5
- CALLBACK_NAMES = [:before_validation]
6
-
7
- def setup_callbacks(base)
8
- base.class_eval do
9
- CALLBACK_NAMES.each do |callback_name|
10
- public_send(callback_name) { Estate::ActiveRecord.validate_state_changes(self) }
11
- end
12
- end
13
- end
14
-
15
- def validate_state_changes(instance)
16
- from_state = instance.public_send("#{Estate::Configuration.column_name}_was")
17
- to_state = instance.public_send(Estate::Configuration.column_name)
18
-
19
- if from_state == to_state
20
- if to_state.nil?
21
- if Estate::Configuration.allow_empty_initial_state
22
- # OK
23
- else
24
- raise(StandardError, "empty `#{Estate::Configuration.column_name}` is not allowed")
25
- end
26
- end
27
- else
28
- # TODO: check to_state.nil?
29
- if Estate::StateMachine.state_exists?(to_state)
30
- if Estate::StateMachine.transition_exists?(from: from_state, to: to_state)
31
- else
32
- instance.errors.add(Estate::Configuration.column_name, message: "transition from `#{from_state}` to `#{to_state}` is not allowed")
33
- end
34
- else
35
- instance.errors.add(:base, "state `#{to_state}` is not defined")
36
- # raise(StandardError, "state `#{to_state}` is not defined")
37
- end
38
- end
39
- end
40
-
41
- module_function :setup_callbacks, :validate_state_changes
42
- end
43
- end