enum_machine 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +18 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +134 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/enum_machine/build_attribute.rb +123 -0
- data/lib/enum_machine/build_class.rb +41 -0
- data/lib/enum_machine/driver_active_record.rb +59 -0
- data/lib/enum_machine/driver_simple_class.rb +52 -0
- data/lib/enum_machine/machine.rb +108 -0
- data/lib/enum_machine/version.rb +7 -0
- data/lib/enum_machine.rb +23 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e3e8eb3957fd71860a322260031d255c8034c53a0d263bd43d68911bcf97507f
|
4
|
+
data.tar.gz: 05306e2ad610425b729a1716ee854614e697acc4dbd0cf6a9d85a476dd1f5209
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea4de98d78c4cb4d0a67655f6c6bcce58f3168696e43b311a5e23873caf49fa4624745de266c852ab34f5edc892c5a7d4459803e772669522d6cf04fa63dff28
|
7
|
+
data.tar.gz: 9233aa651cd9efb8ed0c2cbb16172da70d99fb1b919ba0d4a9146b6c8c312ec85d0d12fc183af82bb3b90f0cdfb4af78c55305f3f53b78a73cd546bb41ac0b7a
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
inherit_gem:
|
2
|
+
rubocop-gp:
|
3
|
+
- ./config/default.yml
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
TargetRubyVersion: 2.6
|
7
|
+
|
8
|
+
Rails/ApplicationRecord:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Metrics/ParameterLists:
|
12
|
+
CountKeywordArgs: false
|
13
|
+
|
14
|
+
Style/Next:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/ClassVars:
|
18
|
+
Enabled: false
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem 'dry-types'
|
8
|
+
gem 'priscilla', github: 'corp-gp/priscilla'
|
9
|
+
gem 'pry', '~> 0.12'
|
10
|
+
gem 'rake', '~> 13.0'
|
11
|
+
gem 'rspec', '~> 3.9'
|
12
|
+
gem 'rubocop-gp', github: 'corp-gp/rubocop-gp'
|
13
|
+
gem 'sqlite3', '~> 1.4'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/corp-gp/priscilla.git
|
3
|
+
revision: 46ba7460df8209dfe80e344beed07dfe56648aaf
|
4
|
+
specs:
|
5
|
+
priscilla (1.0.3)
|
6
|
+
colorize (~> 0.7)
|
7
|
+
rumoji (~> 0.3)
|
8
|
+
|
9
|
+
GIT
|
10
|
+
remote: https://github.com/corp-gp/rubocop-gp.git
|
11
|
+
revision: 4d390b6cdd2b27f151fe0535bd266525fbf8f25d
|
12
|
+
specs:
|
13
|
+
rubocop-gp (0.0.2)
|
14
|
+
rubocop
|
15
|
+
rubocop-performance
|
16
|
+
rubocop-rails
|
17
|
+
rubocop-rspec
|
18
|
+
|
19
|
+
PATH
|
20
|
+
remote: .
|
21
|
+
specs:
|
22
|
+
enum_machine (0.1.0)
|
23
|
+
activemodel (~> 6.0)
|
24
|
+
activerecord (~> 6.0)
|
25
|
+
activesupport (~> 6.0)
|
26
|
+
|
27
|
+
GEM
|
28
|
+
remote: https://rubygems.org/
|
29
|
+
specs:
|
30
|
+
activemodel (6.1.4.6)
|
31
|
+
activesupport (= 6.1.4.6)
|
32
|
+
activerecord (6.1.4.6)
|
33
|
+
activemodel (= 6.1.4.6)
|
34
|
+
activesupport (= 6.1.4.6)
|
35
|
+
activesupport (6.1.4.6)
|
36
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
37
|
+
i18n (>= 1.6, < 2)
|
38
|
+
minitest (>= 5.1)
|
39
|
+
tzinfo (~> 2.0)
|
40
|
+
zeitwerk (~> 2.3)
|
41
|
+
ast (2.4.2)
|
42
|
+
coderay (1.1.3)
|
43
|
+
colorize (0.8.1)
|
44
|
+
concurrent-ruby (1.1.9)
|
45
|
+
diff-lcs (1.5.0)
|
46
|
+
dry-configurable (0.14.0)
|
47
|
+
concurrent-ruby (~> 1.0)
|
48
|
+
dry-core (~> 0.6)
|
49
|
+
dry-container (0.9.0)
|
50
|
+
concurrent-ruby (~> 1.0)
|
51
|
+
dry-configurable (~> 0.13, >= 0.13.0)
|
52
|
+
dry-core (0.7.1)
|
53
|
+
concurrent-ruby (~> 1.0)
|
54
|
+
dry-inflector (0.2.1)
|
55
|
+
dry-logic (1.2.0)
|
56
|
+
concurrent-ruby (~> 1.0)
|
57
|
+
dry-core (~> 0.5, >= 0.5)
|
58
|
+
dry-types (1.5.1)
|
59
|
+
concurrent-ruby (~> 1.0)
|
60
|
+
dry-container (~> 0.3)
|
61
|
+
dry-core (~> 0.5, >= 0.5)
|
62
|
+
dry-inflector (~> 0.1, >= 0.1.2)
|
63
|
+
dry-logic (~> 1.0, >= 1.0.2)
|
64
|
+
i18n (1.9.1)
|
65
|
+
concurrent-ruby (~> 1.0)
|
66
|
+
method_source (1.0.0)
|
67
|
+
minitest (5.15.0)
|
68
|
+
parallel (1.21.0)
|
69
|
+
parser (3.1.0.0)
|
70
|
+
ast (~> 2.4.1)
|
71
|
+
pry (0.14.1)
|
72
|
+
coderay (~> 1.1)
|
73
|
+
method_source (~> 1.0)
|
74
|
+
rack (2.2.3)
|
75
|
+
rainbow (3.1.1)
|
76
|
+
rake (13.0.6)
|
77
|
+
regexp_parser (2.2.1)
|
78
|
+
rexml (3.2.5)
|
79
|
+
rspec (3.11.0)
|
80
|
+
rspec-core (~> 3.11.0)
|
81
|
+
rspec-expectations (~> 3.11.0)
|
82
|
+
rspec-mocks (~> 3.11.0)
|
83
|
+
rspec-core (3.11.0)
|
84
|
+
rspec-support (~> 3.11.0)
|
85
|
+
rspec-expectations (3.11.0)
|
86
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
87
|
+
rspec-support (~> 3.11.0)
|
88
|
+
rspec-mocks (3.11.0)
|
89
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
90
|
+
rspec-support (~> 3.11.0)
|
91
|
+
rspec-support (3.11.0)
|
92
|
+
rubocop (1.25.1)
|
93
|
+
parallel (~> 1.10)
|
94
|
+
parser (>= 3.1.0.0)
|
95
|
+
rainbow (>= 2.2.2, < 4.0)
|
96
|
+
regexp_parser (>= 1.8, < 3.0)
|
97
|
+
rexml
|
98
|
+
rubocop-ast (>= 1.15.1, < 2.0)
|
99
|
+
ruby-progressbar (~> 1.7)
|
100
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
101
|
+
rubocop-ast (1.15.1)
|
102
|
+
parser (>= 3.0.1.1)
|
103
|
+
rubocop-performance (1.13.2)
|
104
|
+
rubocop (>= 1.7.0, < 2.0)
|
105
|
+
rubocop-ast (>= 0.4.0)
|
106
|
+
rubocop-rails (2.13.2)
|
107
|
+
activesupport (>= 4.2.0)
|
108
|
+
rack (>= 1.1)
|
109
|
+
rubocop (>= 1.7.0, < 2.0)
|
110
|
+
rubocop-rspec (2.8.0)
|
111
|
+
rubocop (~> 1.19)
|
112
|
+
ruby-progressbar (1.11.0)
|
113
|
+
rumoji (0.5.0)
|
114
|
+
sqlite3 (1.4.2)
|
115
|
+
tzinfo (2.0.4)
|
116
|
+
concurrent-ruby (~> 1.0)
|
117
|
+
unicode-display_width (2.1.0)
|
118
|
+
zeitwerk (2.5.4)
|
119
|
+
|
120
|
+
PLATFORMS
|
121
|
+
x86_64-linux
|
122
|
+
|
123
|
+
DEPENDENCIES
|
124
|
+
dry-types
|
125
|
+
enum_machine!
|
126
|
+
priscilla!
|
127
|
+
pry (~> 0.12)
|
128
|
+
rake (~> 13.0)
|
129
|
+
rspec (~> 3.9)
|
130
|
+
rubocop-gp!
|
131
|
+
sqlite3 (~> 1.4)
|
132
|
+
|
133
|
+
BUNDLED WITH
|
134
|
+
2.2.29
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Ermolaev Andrey
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# EnumMachine
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/enum_machine`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'enum_machine'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install enum_machine
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/enum_machine.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "enum_machine"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EnumMachine
|
4
|
+
module BuildAttribute
|
5
|
+
|
6
|
+
def self.call(attr:, read_method:, enum_values:, i18n_scope:, machine: nil, aliases_keys: {})
|
7
|
+
parent_attr = "@parent.#{read_method}"
|
8
|
+
|
9
|
+
Class.new do
|
10
|
+
def initialize(parent)
|
11
|
+
@parent = parent
|
12
|
+
end
|
13
|
+
|
14
|
+
define_method(:machine) { machine } if machine
|
15
|
+
|
16
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
17
|
+
# def to_s
|
18
|
+
# @parent.__state
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# def inspect
|
22
|
+
# '<enum_machine :state>'
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# def ==(other)
|
26
|
+
# raise EnumMachine::Error, "use `state.\#{other}?` instead `state == '\#{other}'`"
|
27
|
+
# end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
#{parent_attr}
|
31
|
+
end
|
32
|
+
|
33
|
+
def inspect
|
34
|
+
'<enum_machine :#{attr}>'
|
35
|
+
end
|
36
|
+
|
37
|
+
def ==(other)
|
38
|
+
raise EnumMachine::Error, "use `#{attr}.\#{other}?` instead `#{attr} == '\#{other}'`"
|
39
|
+
end
|
40
|
+
RUBY
|
41
|
+
|
42
|
+
enum_values.each do |enum_value|
|
43
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
44
|
+
# def active?
|
45
|
+
# @parent.__state == 'active'
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# def in?(values)
|
49
|
+
# values.include?(@parent.__state)
|
50
|
+
# end
|
51
|
+
|
52
|
+
def #{enum_value}?
|
53
|
+
#{parent_attr} == '#{enum_value}'
|
54
|
+
end
|
55
|
+
|
56
|
+
def in?(values)
|
57
|
+
values.include?(#{parent_attr})
|
58
|
+
end
|
59
|
+
RUBY
|
60
|
+
|
61
|
+
if machine&.transitions?
|
62
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
63
|
+
# def can_active?
|
64
|
+
# machine.possible_transitions(@parent.__state).include?('canceled')
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# def to_canceled!
|
68
|
+
# @parent.update!('state' => 'canceled')
|
69
|
+
# end
|
70
|
+
|
71
|
+
def can_#{enum_value}?
|
72
|
+
machine.possible_transitions(#{parent_attr}).include?('#{enum_value}')
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_#{enum_value}!
|
76
|
+
@parent.update!('#{attr}' => '#{enum_value}')
|
77
|
+
end
|
78
|
+
RUBY
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
if machine&.transitions?
|
83
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
84
|
+
# def possible_transitions
|
85
|
+
# machine.possible_transitions('active')
|
86
|
+
# end
|
87
|
+
|
88
|
+
def possible_transitions
|
89
|
+
machine.possible_transitions(#{parent_attr})
|
90
|
+
end
|
91
|
+
RUBY
|
92
|
+
end
|
93
|
+
|
94
|
+
aliases_keys.each do |key|
|
95
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
96
|
+
# def forming?
|
97
|
+
# @parent.class::State.forming.include?('active')
|
98
|
+
# end
|
99
|
+
|
100
|
+
def #{key}?
|
101
|
+
@parent.class::State.#{key}.include?(#{parent_attr})
|
102
|
+
end
|
103
|
+
RUBY
|
104
|
+
end
|
105
|
+
|
106
|
+
if i18n_scope
|
107
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
108
|
+
# def i18n
|
109
|
+
# enum_value = @parent.__state
|
110
|
+
# ::I18n.t(enum_value, scope: "enums.product.state", default: enum_value)
|
111
|
+
# end
|
112
|
+
|
113
|
+
def i18n
|
114
|
+
enum_value = #{parent_attr}
|
115
|
+
::I18n.t(enum_value, scope: "enums.#{i18n_scope}", default: enum_value)
|
116
|
+
end
|
117
|
+
RUBY
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EnumMachine
|
4
|
+
class BuildClass
|
5
|
+
|
6
|
+
attr_reader :values
|
7
|
+
|
8
|
+
def initialize(values, aliases = {})
|
9
|
+
@values = values
|
10
|
+
@values.each { |v| memo_attr(v, v) }
|
11
|
+
aliases.each { |k, v| memo_attr(k, v) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def i18n_for(name)
|
15
|
+
::I18n.t(name, scope: "enums.#{i18n_scope}", default: name)
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(name)
|
19
|
+
name_s = name.to_s
|
20
|
+
return super unless name_s.include?('__')
|
21
|
+
|
22
|
+
array_values = name_s.split('__').freeze
|
23
|
+
|
24
|
+
unless (unexists_values = array_values - values).empty?
|
25
|
+
raise EnumMachine::Error, "enums #{unexists_values} not exists"
|
26
|
+
end
|
27
|
+
|
28
|
+
memo_attr(name_s, array_values)
|
29
|
+
end
|
30
|
+
|
31
|
+
def respond_to_missing?(name_s, include_all)
|
32
|
+
name_s.include?('__') || super
|
33
|
+
end
|
34
|
+
|
35
|
+
private def memo_attr(name, value)
|
36
|
+
self.class.attr_reader(name)
|
37
|
+
instance_variable_set("@#{name}", value)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EnumMachine
|
4
|
+
module DriverActiveRecord
|
5
|
+
|
6
|
+
def enum_machine(attr, enum_values, i18n_scope: nil, &block)
|
7
|
+
klass = self
|
8
|
+
|
9
|
+
attr_klass_name = attr.to_s.capitalize
|
10
|
+
read_method = "_read_attribute('#{attr}')"
|
11
|
+
i18n_scope ||= "#{klass.base_class.to_s.underscore}.#{attr}"
|
12
|
+
|
13
|
+
machine = Machine.new(enum_values)
|
14
|
+
machine.instance_eval(&block) if block
|
15
|
+
aliases = machine.instance_variable_get(:@aliases)
|
16
|
+
|
17
|
+
if machine.transitions?
|
18
|
+
klass.class_variable_set("@@#{attr}_machine", machine)
|
19
|
+
|
20
|
+
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1 # rubocop:disable Style/DocumentDynamicEvalDefinition
|
21
|
+
after_validation do
|
22
|
+
unless (attr_changes = changes['#{attr}']).blank?
|
23
|
+
@@#{attr}_machine.fetch_before_transitions(attr_changes).each { |i| i.call(self) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
after_save do
|
27
|
+
unless (attr_changes = previous_changes['#{attr}']).blank?
|
28
|
+
@@#{attr}_machine.fetch_after_transitions(attr_changes).each { |i| i.call(self) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
RUBY
|
32
|
+
end
|
33
|
+
|
34
|
+
klass.const_set attr_klass_name, BuildClass.new(enum_values, aliases)
|
35
|
+
|
36
|
+
attribute_klass =
|
37
|
+
BuildAttribute.call(
|
38
|
+
attr: attr,
|
39
|
+
read_method: read_method,
|
40
|
+
enum_values: enum_values,
|
41
|
+
i18n_scope: i18n_scope,
|
42
|
+
machine: machine,
|
43
|
+
aliases_keys: aliases.keys,
|
44
|
+
)
|
45
|
+
klass.class_variable_set("@@#{attr}_attribute", attribute_klass)
|
46
|
+
|
47
|
+
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
48
|
+
# def state
|
49
|
+
# @state_enum ||= @@state_attribute.new(self)
|
50
|
+
# end
|
51
|
+
|
52
|
+
def #{attr}
|
53
|
+
@#{attr}_enum ||= @@#{attr}_attribute.new(self)
|
54
|
+
end
|
55
|
+
RUBY
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EnumMachine
|
4
|
+
module DriverSimpleClass
|
5
|
+
|
6
|
+
# include EnumMachine[
|
7
|
+
# state: { enum: %w[choice in_delivery], i18n_scope: 'line_item.state' },
|
8
|
+
# color: { enum: %w[red green yellow] },
|
9
|
+
# type: { enum: %w[CartType BookmarkType] },
|
10
|
+
# ]
|
11
|
+
def self.call(args)
|
12
|
+
Module.new do
|
13
|
+
define_singleton_method(:included) do |klass|
|
14
|
+
args.each do |attr, params|
|
15
|
+
enum_values = params.fetch(:enum)
|
16
|
+
i18n_scope = params.fetch(:i18n_scope, nil)
|
17
|
+
enum_class = params.fetch(:enum_class, true)
|
18
|
+
|
19
|
+
attr_klass_name = attr.to_s.capitalize
|
20
|
+
read_method = "__#{attr}"
|
21
|
+
|
22
|
+
attribute_klass =
|
23
|
+
BuildAttribute.call(
|
24
|
+
attr: attr,
|
25
|
+
read_method: read_method,
|
26
|
+
enum_values: enum_values,
|
27
|
+
i18n_scope: i18n_scope,
|
28
|
+
)
|
29
|
+
|
30
|
+
klass.class_variable_set("@@#{attr}_attribute", attribute_klass)
|
31
|
+
|
32
|
+
klass.alias_method read_method, attr
|
33
|
+
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
34
|
+
# def state
|
35
|
+
# @state_enum ||= @@state_attribute.new(self)
|
36
|
+
# end
|
37
|
+
|
38
|
+
def #{attr}
|
39
|
+
@#{attr}_enum ||= @@#{attr}_attribute.new(self)
|
40
|
+
end
|
41
|
+
RUBY
|
42
|
+
|
43
|
+
next unless enum_class
|
44
|
+
|
45
|
+
klass.const_set attr_klass_name, BuildClass.new(enum_values)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EnumMachine
|
4
|
+
class Machine
|
5
|
+
|
6
|
+
attr_reader :enum_values
|
7
|
+
|
8
|
+
def initialize(enum_values)
|
9
|
+
@enum_values = enum_values
|
10
|
+
@transitions = {}
|
11
|
+
@before_transition = {}
|
12
|
+
@after_transition = {}
|
13
|
+
@aliases = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# public api
|
17
|
+
# transitions('s1' => 's2', %w[s3 s3] => 's4')
|
18
|
+
def transitions(from__to_hash)
|
19
|
+
validate_state!(from__to_hash)
|
20
|
+
|
21
|
+
from__to_hash.each do |from_arr, to|
|
22
|
+
array_wrap(from_arr).each do |from|
|
23
|
+
@transitions[from] ||= []
|
24
|
+
@transitions[from] << to
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# public api
|
30
|
+
# before_transition('s1' => 's4')
|
31
|
+
# before_transition(%w[s1 s2] => %w[s3 s4])
|
32
|
+
def before_transition(from__to_hash, &block)
|
33
|
+
validate_state!(from__to_hash)
|
34
|
+
|
35
|
+
from, to = from__to_hash.to_a.first
|
36
|
+
array_wrap(from).product(Array(to)).each do |from_pair_to|
|
37
|
+
valid_transition!(from_pair_to)
|
38
|
+
@before_transition[from_pair_to] ||= []
|
39
|
+
@before_transition[from_pair_to] << block
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# public api
|
44
|
+
# after_transition('s1' => 's4')
|
45
|
+
# after_transition(%w[s1 s2] => %w[s3 s4])
|
46
|
+
def after_transition(from__to_hash, &block)
|
47
|
+
validate_state!(from__to_hash)
|
48
|
+
|
49
|
+
from, to = from__to_hash.to_a.first
|
50
|
+
array_wrap(from).product(Array(to)).each do |from_pair_to|
|
51
|
+
valid_transition!(from_pair_to)
|
52
|
+
@after_transition[from_pair_to] ||= []
|
53
|
+
@after_transition[from_pair_to] << block
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# public api
|
58
|
+
def all
|
59
|
+
enum_values
|
60
|
+
end
|
61
|
+
|
62
|
+
def aliases(hash)
|
63
|
+
@aliases = hash
|
64
|
+
end
|
65
|
+
|
66
|
+
def transitions?
|
67
|
+
@transitions.present?
|
68
|
+
end
|
69
|
+
|
70
|
+
# internal api
|
71
|
+
def fetch_before_transitions(from__to)
|
72
|
+
valid_transition!(from__to)
|
73
|
+
@before_transition.fetch(from__to, [])
|
74
|
+
end
|
75
|
+
|
76
|
+
# internal api
|
77
|
+
def fetch_after_transitions(from__to)
|
78
|
+
@after_transition.fetch(from__to, [])
|
79
|
+
end
|
80
|
+
|
81
|
+
# internal api
|
82
|
+
def possible_transitions(from)
|
83
|
+
@transitions.fetch(from, [])
|
84
|
+
end
|
85
|
+
|
86
|
+
private def validate_state!(object_with_values)
|
87
|
+
unless (undefined = object_with_values.to_a.flatten - enum_values - [nil]).empty?
|
88
|
+
raise EnumMachine::Error, "values #{undefined} not defined in enum_machine"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
private def valid_transition!(from_pair_to)
|
93
|
+
from, to = from_pair_to
|
94
|
+
unless @transitions[from]&.include?(to)
|
95
|
+
raise EnumMachine::Error, "transition #{from} => #{to} not defined in enum_machine"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
private def array_wrap(value)
|
100
|
+
if value.nil?
|
101
|
+
[nil]
|
102
|
+
else
|
103
|
+
Array(value)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
data/lib/enum_machine.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'enum_machine/version'
|
4
|
+
require_relative 'enum_machine/driver_simple_class'
|
5
|
+
require_relative 'enum_machine/build_attribute'
|
6
|
+
require_relative 'enum_machine/build_class'
|
7
|
+
require_relative 'enum_machine/machine'
|
8
|
+
require 'active_support'
|
9
|
+
|
10
|
+
module EnumMachine
|
11
|
+
|
12
|
+
class Error < StandardError; end
|
13
|
+
|
14
|
+
def self.[](args)
|
15
|
+
DriverSimpleClass.call(args)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveSupport.on_load(:active_record) do
|
21
|
+
require_relative 'enum_machine/driver_active_record'
|
22
|
+
ActiveRecord::Base.extend(EnumMachine::DriverActiveRecord)
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_machine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ermolaev Andrey
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '6.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '6.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '6.0'
|
55
|
+
description: state machine for enums
|
56
|
+
email:
|
57
|
+
- andruhafirst@yandex.ru
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".rspec"
|
63
|
+
- ".rubocop.yml"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/enum_machine.rb
|
72
|
+
- lib/enum_machine/build_attribute.rb
|
73
|
+
- lib/enum_machine/build_class.rb
|
74
|
+
- lib/enum_machine/driver_active_record.rb
|
75
|
+
- lib/enum_machine/driver_simple_class.rb
|
76
|
+
- lib/enum_machine/machine.rb
|
77
|
+
- lib/enum_machine/version.rb
|
78
|
+
homepage: https://github.com/corp-gp/enum_machine
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata:
|
82
|
+
allowed_push_host: https://rubygems.org
|
83
|
+
homepage_uri: https://github.com/corp-gp/enum_machine
|
84
|
+
source_code_uri: https://github.com/corp-gp/enum_machine
|
85
|
+
rubygems_mfa_required: 'true'
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 2.6.0
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubygems_version: 3.1.6
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: state machine for enums
|
105
|
+
test_files: []
|