estate 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e432065bf5d5370925a9ba65ca110ebad8c4c3810feaa2302c281d43b4d93426
4
+ data.tar.gz: cd71905ea5af259bc6b970cd91bb215d16934bb76892815439876ca91814d199
5
+ SHA512:
6
+ metadata.gz: c68e50c0e42ebbf1700d9ee93cbcdfd24d1e33a289eb674f1346148035fb92d9bedb492331ddc611f4c987c0699895fa28d74274668c8ea0930f199fe3e377a9
7
+ data.tar.gz: 60fd96c4b66633ee872546c82d087f433532e32ec0cbe89a3df56edfd636cfcc93e596f4f2010f9115281907d96727b3439fa762f72c71a118b92cbbbfd68e7c
data/LICENSE.txt ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2023 Igor Korepanov
2
+
3
+ MIT License
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
data/README.md ADDED
File without changes
@@ -0,0 +1,43 @@
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
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Estate
4
+ module Configuration
5
+ class << self
6
+ def init_config(column_name:, allow_empty_initial_state:)
7
+ @column_name = column_name
8
+ @allow_empty_initial_state = allow_empty_initial_state
9
+ end
10
+
11
+ attr_reader :column_name, :allow_empty_initial_state
12
+ end
13
+
14
+ module Defaults
15
+ COLUMN_NAME = :state
16
+ ALLOW_EMPTY_INITIAL_STATE = false
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Estate
4
+ def self.included(base)
5
+ base.extend Estate::ClassMethods
6
+
7
+ Estate::Requirements.check_requirements(base)
8
+ Estate::ActiveRecord.setup_callbacks(base)
9
+ Estate::StateMachine.create_store
10
+
11
+ # TODO: make sure inheritance (aka subclassing) works with AASM
12
+ super
13
+ end
14
+
15
+ module ClassMethods
16
+ 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)
19
+
20
+ yield if block_given?
21
+ end
22
+
23
+ def state(name)
24
+ if Estate::StateMachine.state_exists?(name)
25
+ raise(StandardError, "state `:#{name}` is already defined")
26
+ else
27
+ Estate::StateMachine.register_state(name)
28
+ end
29
+ end
30
+
31
+ 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
38
+ if Estate::StateMachine.transition_exists?(from: from, to: to)
39
+ raise(StandardError, "`transition from: :#{from}, to: :#{to}` already defined")
40
+ end
41
+
42
+ Estate::StateMachine.register_transition(from: from, to: to)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Estate
4
+ module Requirements
5
+ def check_requirements(base)
6
+ ancestors = base.ancestors.map {|klass| klass.to_s}
7
+
8
+ unless ancestors.include?("ActiveRecord::Base")
9
+ raise(StandardError, "Estate requires ActiveRecord")
10
+ end
11
+ end
12
+
13
+ module_function :check_requirements
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Estate
4
+ class StateMachine
5
+ class << self
6
+ def create_store
7
+ @states = {}
8
+ @transitions = {}
9
+ end
10
+
11
+ def state_exists?(state)
12
+ !state.nil? && states.key?(state.to_sym)
13
+ end
14
+
15
+ def register_state(state)
16
+ case state
17
+ when Symbol
18
+ states[state] = nil
19
+ when String
20
+ states[state.to_sym] = nil
21
+ else
22
+ raise(ArgumentError, 'State must be a Symbol or a String')
23
+ end
24
+ end
25
+
26
+ def transition_exists?(from:, to:)
27
+ # TODO: validate from and to
28
+ transition_key = { from: from.to_sym, to: to.to_sym }
29
+ transitions.key?(transition_key)
30
+ end
31
+
32
+ def register_transition(from:, to:)
33
+ # TODO: validate from and to
34
+ transition_key = { from: from.to_sym, to: to.to_sym }
35
+ transitions[transition_key] = nil
36
+ end
37
+
38
+ attr_reader :states, :transitions
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Estate
4
+ VERSION = '0.0.1'
5
+ end
data/lib/estate.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'estate/version'
4
+ require 'estate/requirements'
5
+ require 'estate/configuration'
6
+ require 'estate/state_machine'
7
+ require 'estate/active_record'
8
+ require 'estate/estate'
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: estate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Igor Korepanov
8
+ autorequire:
9
+ bindir: bin
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
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE.txt
20
+ - README.md
21
+ - lib/estate.rb
22
+ - lib/estate/active_record.rb
23
+ - lib/estate/configuration.rb
24
+ - lib/estate/estate.rb
25
+ - lib/estate/requirements.rb
26
+ - lib/estate/state_machine.rb
27
+ - lib/estate/version.rb
28
+ homepage: https://rubygems.org/gems/estate
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.2.22
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Estate
51
+ test_files: []