enum_machine 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aee9e7e0ece2e73f55fdd9c7180c3babfc1b2bed804cb3475fb1867b0dba757d
4
- data.tar.gz: a9b418f9678c7567baf43d84ac427249580fd4bc976b616677ecb52c89b082ec
3
+ metadata.gz: a93f18555b77dbf6370ff79f6e8a541a60f0b65e70a97673dca07c87ab6a83e8
4
+ data.tar.gz: 4bcf47330cc5492f55fe119d04a814caadfeb47becceb4e5bb9f408976255842
5
5
  SHA512:
6
- metadata.gz: b09927e13245d01a177fbc80b38714021d8c74c2d28a9b3538a3290e7aeb9b298c409e0571fcd9f5db92ddf152eeab8d15edc153315bd75884efadfa77c7b1db
7
- data.tar.gz: 422c5c3de070e51c9314dab1fcb724fd20c3c256433026c32bae4288757438dd51cefe43236ff5256af1577a16a06f343276ea7d4d26f620d433e17e89b2a3f9
6
+ metadata.gz: 4cf9904e3c40b0a864ff4fde87a6e0b5c841b92c3279890ee15d18cf7191361f96a96dcf4ba67892b2f541f30072b8c677d8098b77965bb030f07a585ee46193
7
+ data.tar.gz: c93349f003295b86b1414765d2e844d3d7025c9d6b64da3e5a023eb86765bd627dc47a8c18a0d7ed7e636b9821ac596aee5c3e4d508c676fcdb2b79f406f3f46
data/.rubocop.yml CHANGED
@@ -24,3 +24,6 @@ Gp/UnsafeYamlMarshal:
24
24
  Enabled: true
25
25
  Exclude:
26
26
  - spec/**/*.rb
27
+
28
+ Gp/OptArgParameters:
29
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -73,7 +73,7 @@ GEM
73
73
  zeitwerk (~> 2.6)
74
74
  i18n (1.14.6)
75
75
  concurrent-ruby (~> 1.0)
76
- json (2.8.2)
76
+ json (2.9.0)
77
77
  language_server-protocol (3.17.0.3)
78
78
  logger (1.6.1)
79
79
  method_source (1.1.0)
@@ -89,7 +89,7 @@ GEM
89
89
  rack (3.1.8)
90
90
  rainbow (3.1.1)
91
91
  rake (13.2.1)
92
- regexp_parser (2.9.2)
92
+ regexp_parser (2.9.3)
93
93
  rspec (3.13.0)
94
94
  rspec-core (~> 3.13.0)
95
95
  rspec-expectations (~> 3.13.0)
@@ -103,17 +103,17 @@ GEM
103
103
  diff-lcs (>= 1.2.0, < 2.0)
104
104
  rspec-support (~> 3.13.0)
105
105
  rspec-support (3.13.1)
106
- rubocop (1.69.0)
106
+ rubocop (1.69.2)
107
107
  json (~> 2.3)
108
108
  language_server-protocol (>= 3.17.0)
109
109
  parallel (~> 1.10)
110
110
  parser (>= 3.3.0.2)
111
111
  rainbow (>= 2.2.2, < 4.0)
112
- regexp_parser (>= 2.4, < 3.0)
113
- rubocop-ast (>= 1.36.1, < 2.0)
112
+ regexp_parser (>= 2.9.3, < 3.0)
113
+ rubocop-ast (>= 1.36.2, < 2.0)
114
114
  ruby-progressbar (~> 1.7)
115
115
  unicode-display_width (>= 2.4.0, < 4.0)
116
- rubocop-ast (1.36.1)
116
+ rubocop-ast (1.37.0)
117
117
  parser (>= 3.3.1.0)
118
118
  rubocop-capybara (2.21.0)
119
119
  rubocop (~> 1.41)
@@ -20,7 +20,7 @@ module EnumMachine
20
20
  end
21
21
 
22
22
  if i18n_scope
23
- def self.values_for_form(specific_values = nil) # rubocop:disable Gp/OptArgParameters
23
+ def self.values_for_form(specific_values = nil)
24
24
  (specific_values || values).map { |v| [human_name_for(v), v] }
25
25
  end
26
26
 
@@ -8,7 +8,9 @@ module EnumMachine
8
8
  Class.new(String) do
9
9
  include(value_decorator) if value_decorator
10
10
 
11
- define_method(:machine) { machine } if machine
11
+ define_method(:machine) { machine }
12
+ define_method(:enum_values) { enum_values }
13
+ private :enum_values, :machine
12
14
 
13
15
  def inspect
14
16
  "#<EnumMachine \"#{self}\">"
@@ -27,51 +29,44 @@ module EnumMachine
27
29
  enum_values.each do |enum_value|
28
30
  enum_name = enum_value.underscore
29
31
 
30
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
31
- # def active?
32
- # self == 'active'
33
- # end
32
+ define_method(:"#{enum_name}?") do
33
+ self == enum_value
34
+ end
34
35
 
35
- def #{enum_name}?
36
- self == '#{enum_value}'
36
+ if machine&.transitions?
37
+ define_method(:"can_#{enum_name}?") do
38
+ possible_transitions.include?(enum_value)
37
39
  end
38
- RUBY
40
+ end
41
+ end
39
42
 
40
- if machine&.transitions?
41
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
42
- # def can_active?
43
- # possible_transitions.include?('canceled')
44
- # end
43
+ aliases.each_key do |key|
44
+ define_method(:"#{key}?") do
45
+ machine.fetch_alias(key).include?(self)
46
+ end
47
+ end
45
48
 
46
- def can_#{enum_name}?
47
- possible_transitions.include?('#{enum_value}')
48
- end
49
- RUBY
49
+ if i18n_scope
50
+ full_scope = "enums.#{i18n_scope}"
51
+ define_method(:human_name) do
52
+ ::I18n.t(self, scope: full_scope, default: self)
50
53
  end
51
54
  end
52
55
 
53
- aliases.each_key do |key|
54
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
55
- # def forming?
56
- # machine.fetch_alias('forming').include?(self)
57
- # end
56
+ def respond_to_missing?(method_name, _include_private = false)
57
+ method_name = method_name.name if method_name.is_a?(Symbol)
58
58
 
59
- def #{key}?
60
- machine.fetch_alias('#{key}').include?(self)
61
- end
62
- RUBY
59
+ method_name.end_with?("?") &&
60
+ method_name.include?("__") &&
61
+ (method_name.delete_suffix("?").split("__") - enum_values).empty?
63
62
  end
64
63
 
65
- if i18n_scope
66
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
67
- # def human_name
68
- # ::I18n.t(self, scope: "enums.product.state", default: self)
69
- # end
64
+ def method_missing(method_name)
65
+ return super unless respond_to_missing?(method_name)
70
66
 
71
- def human_name
72
- ::I18n.t(self, scope: "enums.#{i18n_scope}", default: self)
73
- end
74
- RUBY
67
+ m_enums = method_name.name.delete_suffix("?").split("__")
68
+ self.class.define_method(method_name) { m_enums.include?(self) }
69
+ send(method_name)
75
70
  end
76
71
  end
77
72
  end
@@ -4,7 +4,7 @@ module EnumMachine
4
4
  class Machine
5
5
  attr_reader :enum_values, :base_klass, :enum_const_name, :attr_name
6
6
 
7
- def initialize(enum_values, base_klass = nil, enum_const_name = nil, attr_name = nil) # rubocop:disable Gp/OptArgParameters
7
+ def initialize(enum_values, base_klass = nil, enum_const_name = nil, attr_name = nil)
8
8
  @enum_values = enum_values
9
9
  @base_klass = base_klass
10
10
  @enum_const_name = enum_const_name
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EnumMachine
4
- VERSION = "2.0.1"
4
+ VERSION = "2.1.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ermolaev Andrey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-04 00:00:00.000000000 Z
11
+ date: 2024-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel