enum_machine 2.2.0 → 2.3.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: 22866d6d5f37dd78718e7e5eb27dd0066450d8730b821ffb13977888009b6ddd
4
- data.tar.gz: fc4c40aa5ea99a6d14143ebec78d2a40b2c8bce58d21ff5507b4d3d2ee67bcf1
3
+ metadata.gz: 86f84c741963ae08afc9fc2072a816140b13a8f819fecf2739460cf1118ac10d
4
+ data.tar.gz: c8eea28785d2845c8ccaf8892f9c63f8e600054b5e8c74998656f1fbda3ced97
5
5
  SHA512:
6
- metadata.gz: 247ea0d77022905062d7688dd1153037517fa71521c15f51477f66e4f8d368cf008262c0095a1465b068b24da3262ed01334f787504dce9a65265839bffa2726
7
- data.tar.gz: 969db73142c57fcf94f82fa6c1eca59c79310a6c6e0399961fbee835c5da8596802039d699657f306f07ac0f0e64ba5692cb4d3dee0f3d02359313cd61b07bc5
6
+ metadata.gz: db343c2b699f2d00ead6c0ae58a033b3269e4680f55711eb82b136ed0c7092ec3365ce6b5cddd19d28378f2cf1952d67e5e411f6fb97d9a0059ad7ca79a1af5d
7
+ data.tar.gz: 98e5ad6a7e5ecff10a12c96f2d24a1d19b75b7903bd55fa70aec36d628502ed090d0568d0f5e9ddbd941e08804171d4ad0e09d84dc44b3836d33aca55545c33c
data/.rubocop.yml CHANGED
@@ -27,3 +27,9 @@ Gp/UnsafeYamlMarshal:
27
27
 
28
28
  Gp/OptArgParameters:
29
29
  Enabled: false
30
+
31
+ Gp/SpecNestedDuplicatesLetName:
32
+ Enabled: false
33
+
34
+ Rails/IndexBy:
35
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -22,7 +22,7 @@ GIT
22
22
  PATH
23
23
  remote: .
24
24
  specs:
25
- enum_machine (2.2.0)
25
+ enum_machine (2.3.0)
26
26
  activemodel
27
27
  activerecord
28
28
  activesupport
data/README.md CHANGED
@@ -4,12 +4,12 @@
4
4
 
5
5
  You can visualize transitions map with [enum_machine-contrib](https://github.com/corp-gp/enum_machine-contrib)
6
6
 
7
- ## Why is `enum_machine` better then [state_machines](https://github.com/state-machines/state_machines) / [aasm](https://github.com/aasm/aasm)?
7
+ ## Why is `enum_machine` better than [state_machines](https://github.com/state-machines/state_machines) / [aasm](https://github.com/aasm/aasm)?
8
8
 
9
9
  - faster [5x](#Benchmarks)
10
10
  - code lines: `enum_machine` - 348, `AASM` - 2139
11
11
  - namespaced (via attr) by default: `order.state.to_collected`
12
- - [aliases](Aliases)
12
+ - [aliases](#aliases)
13
13
  - guarantees of existing transitions
14
14
  - simple run transitions with callbacks `order.update(state: "collected")` or `order.state.to_collected`
15
15
  - `aasm` / `state_machines` **event driven**, `enum_machine` **state driven**
@@ -59,7 +59,7 @@ gem "enum_machine"
59
59
  ```ruby
60
60
  # With ActiveRecord
61
61
  class Product < ActiveRecord::Base
62
- enum_machine :color, %w[red green]
62
+ enum_machine :color, %w[red green yellow]
63
63
  end
64
64
 
65
65
  # Or with plain class
@@ -67,12 +67,12 @@ class Product
67
67
  # attributes must be defined before including the EnumMachine module
68
68
  attr_accessor :color
69
69
 
70
- include EnumMachine[color: { enum: %w[red green] }]
70
+ include EnumMachine[color: { enum: %w[red green yellow] }]
71
71
  # or reuse from model
72
72
  Product::COLOR.enum_decorator
73
73
  end
74
74
 
75
- Product::COLOR.values # => ["red", "green"]
75
+ Product::COLOR.values # => ["red", "green", "yellow"]
76
76
  Product::COLOR::RED # => "red"
77
77
  Product::COLOR::RED.red? # => true
78
78
  Product::COLOR::RED.human_name # => "Красный"
@@ -85,6 +85,8 @@ product = Product.new
85
85
  product.color # => nil
86
86
  product.color = "red"
87
87
  product.color.red? # => true
88
+ product.color.red__yellow? # => true
89
+ product.color.green__yellow? # => false
88
90
  product.color.human_name # => "Красный"
89
91
  ```
90
92
 
@@ -168,7 +170,7 @@ product = Product.create(state: "created")
168
170
  product.state.possible_transitions # => [nil, "approved"]
169
171
  product.state.can_activated? # => false
170
172
  product.state.to_activated! # => EnumMachine::Error: transition "created" => "activated" not defined in enum_machine
171
- product.state.to_approved! # => true; equal to `product.update!(state: "approve")`
173
+ product.state.to_approved! # => true; equal to `product.update!(state: "approved")`
172
174
  ```
173
175
 
174
176
  #### Skip transitions
@@ -225,7 +227,7 @@ product.color.human_name # => "Красный"
225
227
  I18n scope can be changed with `i18n_scope` option:
226
228
 
227
229
  ```ruby
228
- # For AciveRecord
230
+ # For ActiveRecord
229
231
  class Product < ActiveRecord::Base
230
232
  enum_machine :color, %w[red green], i18n_scope: "users.product"
231
233
  end
@@ -236,6 +238,72 @@ class Product
236
238
  end
237
239
  ```
238
240
 
241
+ ### Standalone enum
242
+
243
+ `EnumMachine.enum` builds an enum class without a host attribute: for computed values,
244
+ virtual attributes, permissions, or constants shared across classes.
245
+
246
+ It provides constants, `values`, lookup `[]`, predicates (including combined `foo__bar?`),
247
+ I18n, and custom decorators:
248
+
249
+ ```ruby
250
+ module ReviewTypeDecorator
251
+ def bonus_value
252
+ case self
253
+ when Comment::REVIEW_TYPE::VIDEO then 100
254
+ when Comment::REVIEW_TYPE::PHOTO then 50
255
+ else 10
256
+ end
257
+ end
258
+ end
259
+
260
+ class Comment < ActiveRecord::Base
261
+ REVIEW_TYPE = EnumMachine.enum(
262
+ %w[text photo video],
263
+ i18n_scope: "comment.review_type",
264
+ value_decorator: ReviewTypeDecorator,
265
+ )
266
+
267
+ # computed, not a database column
268
+ def review_type
269
+ if video_ids.any?
270
+ REVIEW_TYPE::VIDEO
271
+ elsif photo_ids.any?
272
+ REVIEW_TYPE::PHOTO
273
+ else
274
+ REVIEW_TYPE::TEXT
275
+ end
276
+ end
277
+ end
278
+
279
+ # Constants and listing
280
+ Comment::REVIEW_TYPE.values # => ["text", "photo", "video"]
281
+ Comment::REVIEW_TYPE::VIDEO # => "video"
282
+ Comment::REVIEW_TYPE::PHOTO__VIDEO # => ["photo", "video"]
283
+
284
+ # Lookup by string or symbol
285
+ Comment::REVIEW_TYPE["video"] # => "video"
286
+ Comment::REVIEW_TYPE[:video] # => "video"
287
+ Comment::REVIEW_TYPE["unknown"] # => nil
288
+
289
+ # Decorator methods
290
+ Comment::REVIEW_TYPE::VIDEO.bonus_value # => 100
291
+
292
+ # Predicates on computed values
293
+ comment = Comment.new(video_ids: [1])
294
+ comment.review_type.video? # => true
295
+ comment.review_type.photo__video? # => true
296
+ comment.review_type.bonus_value # => 100
297
+
298
+ # Form helpers (when i18n_scope is set)
299
+ Comment::REVIEW_TYPE.values_for_form
300
+ # => [["Text", "text"], ["Photo", "photo"], ["Video", "video"]]
301
+ ```
302
+
303
+ In `ActiveRecord` models, `enum_machine :attribute` relies on a database column for persistence
304
+ and dirty tracking. `EnumMachine.enum` is designed for everything else where you still want
305
+ enum capabilities without an underlying column.
306
+
239
307
  ## Benchmarks
240
308
  [test/performance.rb](../master/test/performance.rb)
241
309
 
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/enum_machine/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "enum_machine"
7
+ spec.version = EnumMachine::VERSION
8
+ spec.authors = ["Ermolaev Andrey"]
9
+ spec.email = ["andruhafirst@yandex.ru"]
10
+
11
+ spec.summary = "fast and siple usage state machine in your app"
12
+ spec.description = "Enum machine is a library for defining enums and setting state machines for attributes in ActiveRecord models and plain Ruby classes."
13
+ spec.homepage = "https://github.com/corp-gp/enum_machine"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/corp-gp/enum_machine"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files =
25
+ Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "activemodel"
35
+ spec.add_dependency "activerecord"
36
+ spec.add_dependency "activesupport"
37
+
38
+ # For more information and examples about making a new gem, checkout our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ spec.metadata["rubygems_mfa_required"] = "true"
41
+ end
@@ -19,8 +19,7 @@ module EnumMachine
19
19
  klass.enum_machine(attr, enum_values, i18n_scope: i18n_scope)
20
20
  else
21
21
  enum_const_name = attr.to_s.upcase
22
- value_class = BuildValueClass.call(enum_values: enum_values, i18n_scope: i18n_scope, value_decorator: value_decorator)
23
- enum_class = BuildEnumClass.call(enum_values: enum_values, i18n_scope: i18n_scope, value_class: value_class)
22
+ enum_class = EnumMachine.enum(enum_values, i18n_scope: i18n_scope, value_decorator: value_decorator)
24
23
 
25
24
  define_methods =
26
25
  Module.new do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EnumMachine
4
- VERSION = "2.2.0"
4
+ VERSION = "2.3.0"
5
5
  end
data/lib/enum_machine.rb CHANGED
@@ -29,6 +29,15 @@ module EnumMachine
29
29
  def self.[](args)
30
30
  DriverSimpleClass.call(args)
31
31
  end
32
+
33
+ # Enum class without a host class: constants, `values`, `[]`, predicates and decorator
34
+ #
35
+ # COLOR = EnumMachine.enum(%w[red green], i18n_scope: "product.color", value_decorator: ColorDecorator)
36
+ # COLOR::RED.red? # => true
37
+ def self.enum(enum_values, i18n_scope: nil, value_decorator: nil)
38
+ value_class = BuildValueClass.call(enum_values: enum_values, i18n_scope: i18n_scope, value_decorator: value_decorator)
39
+ BuildEnumClass.call(enum_values: enum_values, i18n_scope: i18n_scope, value_class: value_class)
40
+ end
32
41
  end
33
42
 
34
43
  ActiveSupport.on_load(:active_record) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ermolaev Andrey
@@ -69,6 +69,7 @@ files:
69
69
  - bin/console
70
70
  - bin/setup
71
71
  - docs/MIGRATING_FROM_RAILS_STRING_ENUM.ru.md
72
+ - enum_machine.gemspec
72
73
  - lib/enum_machine.rb
73
74
  - lib/enum_machine/attribute_persistence_methods.rb
74
75
  - lib/enum_machine/build_enum_class.rb