estate 0.0.1 → 0.0.2

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: f445ae55c16632d7805cd1774f2800eed57ebf16ce63db5ef7e780225ecc52a1
4
+ data.tar.gz: 60527e77ed67584c663bde4504a104c1b9b38e179fdd299e5344bef2031d6bb9
5
5
  SHA512:
6
- metadata.gz: c68e50c0e42ebbf1700d9ee93cbcdfd24d1e33a289eb674f1346148035fb92d9bedb492331ddc611f4c987c0699895fa28d74274668c8ea0930f199fe3e377a9
7
- data.tar.gz: 60fd96c4b66633ee872546c82d087f433532e32ec0cbe89a3df56edfd636cfcc93e596f4f2010f9115281907d96727b3439fa762f72c71a118b92cbbbfd68e7c
6
+ metadata.gz: e27a18f1f5a82b3b2316d559eb7847f400f15b025fad62dc3d1ced8106856d2c8c60014a86af2837e81f03cf6956175af7f2ab0dabf08d0b0d4fb6b7dac37495
7
+ data.tar.gz: a2f52b92fff0482abbc7981ab5685d59b5430e171f37c568f68f8ab72d1ed57d851886b0bbf631c472ee34ced52699203571c1ffd420ee636d94cfa00daf1984
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,89 @@
1
+ # Estate Gem
2
+
3
+ Estate is a Ruby gem designed to simplify state management in ActiveRecord models. The primary focus of this gem is to provide a straightforward way to define states and transitions using a clean syntax, allowing seamless integration into ActiveRecord models.
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
+
21
+ To use the Estate gem, include it in your ActiveRecord 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
+ The default field for storing the state is named state. You can customize this field by providing options to the estate method:
40
+
41
+ ```ruby
42
+ class MyModel < ApplicationRecord
43
+ include Estate
44
+
45
+ estate(column: :custom_state_field, empty_initial_state: true) do
46
+ state :state_1
47
+ state :state_2
48
+ state :state_3
49
+
50
+ transition from: :state_1, to: :state_2
51
+ transition from: :state_2, to: :state_3
52
+ transition from: :state_3, to: :state_1
53
+ end
54
+ end
55
+ ```
56
+
57
+ ## Migration Example
58
+
59
+ ```bash
60
+ bundle exec rails generate migration AddStateToMyModels state:string
61
+ ```
62
+
63
+ This will generate a migration file. Open the migration file and modify it to suit your needs, for example:
64
+
65
+ ```ruby
66
+ class AddStateToMyModels < ActiveRecord::Migration[7.0]
67
+ def change
68
+ add_column :my_models, :state, :string
69
+ end
70
+ end
71
+ ```
72
+
73
+ Run the migration:
74
+
75
+ ```bash
76
+ bundle exec rails db:migrate
77
+ ```
78
+
79
+ ## License
80
+
81
+ Copyright (c) 2023-2024 Igor Korepanov
82
+
83
+ MIT License
84
+
85
+ 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:
86
+
87
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
88
+
89
+ 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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Estate
4
4
  module ActiveRecord
5
- CALLBACK_NAMES = [:before_validation]
5
+ CALLBACK_NAMES = [:before_validation].freeze
6
6
 
7
7
  def setup_callbacks(base)
8
8
  base.class_eval do
@@ -17,27 +17,19 @@ module Estate
17
17
  to_state = instance.public_send(Estate::Configuration.column_name)
18
18
 
19
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
20
+ if to_state.nil? && !Estate::Configuration.allow_empty_initial_state
21
+ raise(StandardError, "empty `#{Estate::Configuration.column_name}` is not allowed")
26
22
  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")
23
+ elsif Estate::StateMachine.state_exists?(to_state) # TODO: check to_state.nil?
24
+ unless Estate::StateMachine.transition_exists?(from: from_state, to: to_state)
25
+ instance.errors.add(Estate::Configuration.column_name,
26
+ message: "transition from `#{from_state}` to `#{to_state}` is not allowed")
37
27
  end
28
+ else
29
+ instance.errors.add(:base, "state `#{to_state}` is not defined")
38
30
  end
39
31
  end
40
32
 
41
33
  module_function :setup_callbacks, :validate_state_changes
42
34
  end
43
- end
35
+ end
@@ -16,4 +16,4 @@ module Estate
16
16
  ALLOW_EMPTY_INITIAL_STATE = false
17
17
  end
18
18
  end
19
- end
19
+ end
data/lib/estate/estate.rb CHANGED
@@ -8,7 +8,6 @@ module Estate
8
8
  Estate::ActiveRecord.setup_callbacks(base)
9
9
  Estate::StateMachine.create_store
10
10
 
11
- # TODO: make sure inheritance (aka subclassing) works with AASM
12
11
  super
13
12
  end
14
13
 
@@ -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,11 @@
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")
10
- end
8
+ raise(StandardError, 'Estate requires ActiveRecord') unless ancestors.include?('ActiveRecord::Base')
11
9
  end
12
10
 
13
11
  module_function :check_requirements
14
12
  end
15
- end
13
+ end
@@ -38,4 +38,4 @@ module Estate
38
38
  attr_reader :states, :transitions
39
39
  end
40
40
  end
41
- end
41
+ 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.0.2'
5
+ end
metadata CHANGED
@@ -1,17 +1,74 @@
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.0.2
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-22 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
+ description: Estate is a Ruby gem designed to simplify state management in ActiveRecord
70
+ models
71
+ email: noemail@example.com
15
72
  executables: []
16
73
  extensions: []
17
74
  extra_rdoc_files: []
@@ -25,7 +82,7 @@ files:
25
82
  - lib/estate/requirements.rb
26
83
  - lib/estate/state_machine.rb
27
84
  - lib/estate/version.rb
28
- homepage: https://rubygems.org/gems/estate
85
+ homepage: https://github.com/igorkorepanov/estate
29
86
  licenses:
30
87
  - MIT
31
88
  metadata: {}
@@ -37,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
94
  requirements:
38
95
  - - ">="
39
96
  - !ruby/object:Gem::Version
40
- version: '0'
97
+ version: '2.7'
41
98
  required_rubygems_version: !ruby/object:Gem::Requirement
42
99
  requirements:
43
100
  - - ">="
@@ -47,5 +104,5 @@ requirements: []
47
104
  rubygems_version: 3.2.22
48
105
  signing_key:
49
106
  specification_version: 4
50
- summary: Estate
107
+ summary: State machine for Rails models
51
108
  test_files: []