estate 0.2.0 → 1.0.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: 4b3da0543146b0a82972f87969bc2278b1deb7ab600965370caf143bd8b342d7
4
- data.tar.gz: e87a26965c47b976746059431196048fb2b03cb829475e5e1eabf48b80054694
3
+ metadata.gz: 71bb34f3ba838cee7a69c1c149e10fa539f074d887503dc97e02818a05c8712b
4
+ data.tar.gz: 97e67199bd544a360a0b7da92a6321144d1bfce380d15463c929c540713ffcce
5
5
  SHA512:
6
- metadata.gz: bd904b3f3dbcf36895fb76c3c76ce05c7cb4af45ab30150fb8d9becd2120c9a7add11ada7051396e3f5cfc7093702221b7f6e77de994e04e347f1018802ce0f3
7
- data.tar.gz: c69b6df5691fcc4104c28e9ce5eb23ad294c54002a8ff32c498750074c7c6bded4bbe7cb0707d69f3abd23ab4f47e67b59ca25571784c008e63c41135620e76e
6
+ metadata.gz: a1efa655c9bf89e4a19bd4b5840e816ad048418c509b267e569e0040f793ab2bfad11b817d7d97b19ee8cf777455da1e040cfb5e0be8abdf36900d10301a093d
7
+ data.tar.gz: 93f161f47890608d3a2ec5a92195524c6fe884a1d3a140557908f0c76402f2effcf9b5b687347b0eec07d414eca7dc2a85bf3ec87e291f6d6fccdf2573dbdcfd
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # Estate Gem
4
4
 
5
- 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.
5
+ Estate is a Ruby gem designed to simplify state management in models, including ActiveRecord, Sequel, as well as plain Ruby objects. The primary focus of this gem is to provide a straightforward way to define states and transitions using a clean syntax.
6
6
 
7
7
  ## Installation
8
8
 
@@ -2,9 +2,10 @@
2
2
 
3
3
  module Estate
4
4
  module Constants
5
- module Orm
5
+ module Adapters
6
6
  ACTIVE_RECORD = 'active_record'
7
7
  SEQUEL = 'sequel'
8
+ PLAIN_RUBY_OBJECT = 'plain_ruby_object'
8
9
  end
9
10
  end
10
11
  end
data/lib/estate/estate.rb CHANGED
@@ -3,9 +3,6 @@
3
3
  module Estate
4
4
  def self.included(base)
5
5
  base.extend Estate::ClassMethods
6
-
7
- Estate::Requirements.check_requirements(base)
8
- Estate::Setup.call(base)
9
6
  end
10
7
 
11
8
  module ClassMethods
@@ -13,6 +10,7 @@ module Estate
13
10
  empty_initial_state: Estate::Configuration::Defaults::ALLOW_EMPTY_INITIAL_STATE,
14
11
  raise_on_error: Estate::Configuration::Defaults::RAISE_ON_ERROR)
15
12
  Estate::StateMachine.init(name, column, empty_initial_state, raise_on_error)
13
+ Estate::Setup.call(self)
16
14
 
17
15
  yield if block_given?
18
16
  end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'estate/logic/common_logic'
4
+ require 'estate/logic/active_record/specific_logic'
5
+
3
6
  module Estate
4
7
  module Logic
5
8
  module ActiveRecord
@@ -9,7 +12,9 @@ module Estate
9
12
  def call(base)
10
13
  base.class_eval do
11
14
  public_send(:before_validation) do
12
- Estate::Logic::Core.call(Estate::Constants::Orm::ACTIVE_RECORD, self)
15
+ extend Estate::Logic::CommonLogic
16
+ extend Estate::Logic::ActiveRecord::SpecificLogic
17
+ validate_state_changes(self, *get_states(self))
13
18
  end
14
19
  end
15
20
  end
@@ -1,13 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'estate/logic/common_logic'
4
-
5
3
  module Estate
6
4
  module Logic
7
5
  module ActiveRecord
8
6
  module SpecificLogic
9
- extend Estate::Logic::CommonLogic
10
-
11
7
  module_function
12
8
 
13
9
  def add_error(instance, message, attribute: :base)
@@ -24,8 +24,8 @@ module Estate
24
24
  from_state.nil? || Estate::StateMachine.transition_exists?(state_machine_name, from_state, to_state)
25
25
  end
26
26
 
27
- def config_for(instance)
28
- state_machine_name = instance.class.name
27
+ def config_for(klass)
28
+ state_machine_name = klass.is_a?(Class) ? klass.name : klass.class.name
29
29
  Estate::StateMachine.state_machines[state_machine_name][:config]
30
30
  end
31
31
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'estate/logic/common_logic'
4
+ require 'estate/logic/plain_ruby_object/specific_logic'
5
+
6
+ module Estate
7
+ module Logic
8
+ module PlainRubyObject
9
+ module Setup
10
+ module_function
11
+
12
+ def call(base)
13
+ base.prepend(Module.new do
14
+ extend Estate::Logic::CommonLogic
15
+
16
+ define_method("#{config_for(base)[:column_name]}=") do |new_value|
17
+ extend Estate::Logic::CommonLogic
18
+ extend Estate::Logic::PlainRubyObject::SpecificLogic
19
+
20
+ validate_state_changes(self, state, new_value)
21
+
22
+ super(new_value)
23
+ end
24
+ end)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Estate
4
+ module Logic
5
+ module PlainRubyObject
6
+ module SpecificLogic
7
+ module_function
8
+
9
+ def add_error(_, message, attribute: :base)
10
+ exception_message = attribute == :base ? message : "#{attribute}: #{message}"
11
+ raise(StandardError, exception_message)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'estate/logic/common_logic'
4
+ require 'estate/logic/sequel/specific_logic'
5
+
3
6
  module Estate
4
7
  module Logic
5
8
  module Sequel
@@ -11,7 +14,9 @@ module Estate
11
14
  def validate
12
15
  super
13
16
 
14
- Estate::Logic::Core.call(Estate::Constants::Orm::SEQUEL, self)
17
+ extend Estate::Logic::CommonLogic
18
+ extend Estate::Logic::Sequel::SpecificLogic
19
+ validate_state_changes(self, *get_states(self))
15
20
  end
16
21
  end
17
22
  end
@@ -1,13 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'estate/logic/common_logic'
4
-
5
3
  module Estate
6
4
  module Logic
7
5
  module Sequel
8
6
  module SpecificLogic
9
- extend Estate::Logic::CommonLogic
10
-
11
7
  module_function
12
8
 
13
9
  # TODO: remove :base
data/lib/estate/setup.rb CHANGED
@@ -5,12 +5,16 @@ module Estate
5
5
  module_function
6
6
 
7
7
  def call(base)
8
- if base.ancestors.map(&:to_s).include? 'ActiveRecord::Base'
8
+ ancestors = base.ancestors.map(&:to_s)
9
+ if ancestors.include? 'ActiveRecord::Base'
9
10
  require File.join(File.dirname(__FILE__), 'logic', 'active_record', 'setup')
10
11
  Estate::Logic::ActiveRecord::Setup.call(base)
11
- else
12
+ elsif ancestors.include? 'Sequel::Model'
12
13
  require File.join(File.dirname(__FILE__), 'logic', 'sequel', 'setup')
13
14
  Estate::Logic::Sequel::Setup.call(base)
15
+ else
16
+ require File.join(File.dirname(__FILE__), 'logic', 'plain_ruby_object', 'setup')
17
+ Estate::Logic::PlainRubyObject::Setup.call(base)
14
18
  end
15
19
  end
16
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Estate
4
- VERSION = '0.2.0'
4
+ VERSION = '1.0.0'
5
5
  end
data/lib/estate.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'estate/configuration'
4
- require 'estate/constants/orm'
4
+ require 'estate/constants/adapters'
5
5
  require 'estate/estate'
6
- require 'estate/logic/core'
7
- require 'estate/requirements'
8
6
  require 'estate/setup'
9
7
  require 'estate/state_machine'
10
8
  require 'estate/version'
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.2.0
4
+ version: 1.0.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-30 00:00:00.000000000 Z
11
+ date: 2024-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -80,9 +80,9 @@ dependencies:
80
80
  - - '='
81
81
  - !ruby/object:Gem::Version
82
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
83
+ description: Estate is a Ruby gem designed to simplify state management in models,
84
+ as well as plain Ruby objects
85
+ email: korepanovigor87@gmail.com
86
86
  executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
@@ -91,15 +91,15 @@ files:
91
91
  - README.md
92
92
  - lib/estate.rb
93
93
  - lib/estate/configuration.rb
94
- - lib/estate/constants/orm.rb
94
+ - lib/estate/constants/adapters.rb
95
95
  - lib/estate/estate.rb
96
96
  - lib/estate/logic/active_record/setup.rb
97
97
  - lib/estate/logic/active_record/specific_logic.rb
98
98
  - lib/estate/logic/common_logic.rb
99
- - lib/estate/logic/core.rb
99
+ - lib/estate/logic/plain_ruby_object/setup.rb
100
+ - lib/estate/logic/plain_ruby_object/specific_logic.rb
100
101
  - lib/estate/logic/sequel/setup.rb
101
102
  - lib/estate/logic/sequel/specific_logic.rb
102
- - lib/estate/requirements.rb
103
103
  - lib/estate/setup.rb
104
104
  - lib/estate/state_machine.rb
105
105
  - lib/estate/version.rb
@@ -125,5 +125,5 @@ requirements: []
125
125
  rubygems_version: 3.2.22
126
126
  signing_key:
127
127
  specification_version: 4
128
- summary: State machine for Rails models
128
+ summary: State machine for Rails models and plain Ruby objects
129
129
  test_files: []
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Estate
4
- module Logic
5
- module Core
6
- module_function
7
-
8
- def call(orm, instance)
9
- require 'estate/logic/common_logic'
10
- require File.join(File.dirname(__FILE__), orm, 'specific_logic')
11
-
12
- extend Estate::Logic::CommonLogic
13
- extend "Estate::Logic::#{orm.classify}::SpecificLogic".safe_constantize
14
-
15
- validate_state_changes(instance, *get_states(instance))
16
- end
17
- end
18
- end
19
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Estate
4
- module Requirements
5
- def check_requirements(base)
6
- ancestors = base.ancestors.map(&:to_s)
7
-
8
- unless 'Sequel::Model'.in?(ancestors) || 'ActiveRecord::Base'.in?(ancestors)
9
- raise(StandardError, 'Estate requires ActiveRecord or Sequel')
10
- end
11
- end
12
-
13
- module_function :check_requirements
14
- end
15
- end