swifter_enum 0.9.0 → 0.9.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 +4 -4
- data/CHANGELOG.md +7 -2
- data/README.md +43 -8
- data/Rakefile +31 -0
- data/lib/swifter_enum/swifter_enum.rb +2 -1
- data/lib/swifter_enum/version.rb +1 -1
- metadata +19 -6
- data/swifter_enum.gemspec +0 -45
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b72cb72dc43be470b8170fd34843f2f2e7ddb4d64a2856c7094b63c0ba091e2
|
4
|
+
data.tar.gz: 791abe5497d7c623e9b5e5ef9554a00f2c242b74b4f0668e018d7cc7be2d0752
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4f5bf8eab4588d23098bf11bab794205369820c0b6900b7dfa9f2d51cc85d489af4a9007a807d65c392b313cfb1201249a12650e4674cc77ed682cbb52f305e
|
7
|
+
data.tar.gz: 0a6226463dd0041d3c4999813b483ca274af966d82665cb0b10c7385ea682012775d835196edd231f2400a542c63f14148da2abfab61c0fda825e2af9ab9e8f9
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
# SwifterEnum
|
2
2
|
|
3
3
|
SwifterEnum is a Ruby gem for creating enumerated types (enums) in Ruby on Rails applications.
|
4
|
-
It is inspired by Swift's enums, and allows for more expressive and feature-rich enums in your models.
|
5
4
|
|
6
|
-
|
5
|
+
It is inspired by Swift's enums, and allows you to keep logic related to your enum directly in your enum class.
|
7
6
|
|
8
7
|
so - after defining
|
9
8
|
|
@@ -14,7 +13,43 @@ so - after defining
|
|
14
13
|
you can then define and access methods on your enum like
|
15
14
|
|
16
15
|
`video.camera.icon`
|
17
|
-
|
16
|
+
|
17
|
+
This avoids helper methods which distribute your enum logic around your application.
|
18
|
+
|
19
|
+
**Before**
|
20
|
+
|
21
|
+
helper method somewhere in the app
|
22
|
+
|
23
|
+
#app/helpers/controller_helper.rb
|
24
|
+
|
25
|
+
def icon_for(camera:)
|
26
|
+
...
|
27
|
+
end
|
28
|
+
|
29
|
+
called with
|
30
|
+
|
31
|
+
icon_for(camera:my_model.camera)
|
32
|
+
|
33
|
+
**After**
|
34
|
+
|
35
|
+
logic encapsluated within the enum class
|
36
|
+
|
37
|
+
#app/models/swifter_enum/camera_enum.rb
|
38
|
+
class CameraEnum < SwifterEnum::Base
|
39
|
+
def self.values
|
40
|
+
{ videographer: 0, handcam: 1 }.freeze
|
41
|
+
end
|
42
|
+
|
43
|
+
def icon
|
44
|
+
...
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
called with
|
49
|
+
|
50
|
+
my_model.camera.icon
|
51
|
+
|
52
|
+
I was prompted to create this gem by reading about enum approaches in [the RailsNotes Newsletter](https://railsnotes.beehiiv.com/p/issue-17-enums-value-objects-field-guide-enum-sort-in-order-of). Like any good programmer, none of those solutions *quite* met my requirements. Hopefully it will be useful. I welcome feedback, fixes and pull requests.
|
18
53
|
|
19
54
|
|
20
55
|
## Installation
|
@@ -23,16 +58,14 @@ Add this line to your application's Gemfile:
|
|
23
58
|
|
24
59
|
gem 'swifter_enum'
|
25
60
|
|
26
|
-
And then execute:
|
27
|
-
|
28
|
-
bundle install
|
29
|
-
|
30
61
|
## Usage
|
31
62
|
|
32
63
|
### Overview
|
33
64
|
|
34
65
|
|
35
|
-
SwifterEnums act like a normal Rails enum - except that instead of returning
|
66
|
+
SwifterEnums act like a normal Rails enum - except that instead of returning string values, they return an instance of your selected class.
|
67
|
+
|
68
|
+
They also have various affordances so that in many cases, you can treat them as if they return symbol values.
|
36
69
|
|
37
70
|
We have a Video ActiveModel with an enum defined by
|
38
71
|
|
@@ -153,6 +186,8 @@ Now replace the definition in your model file with
|
|
153
186
|
|
154
187
|
swifter_enum :album_status, AlbumStatusEnum, prefix: true
|
155
188
|
|
189
|
+
(note - prefix: optional. I'm adding it here because it was an option I used on my original standard Rails enum)
|
190
|
+
|
156
191
|
Optionally, add
|
157
192
|
|
158
193
|
validates :album_status, swifter_enum: true
|
data/Rakefile
CHANGED
@@ -12,4 +12,35 @@ Rake::TestTask.new do |t|
|
|
12
12
|
t.verbose = true
|
13
13
|
end
|
14
14
|
|
15
|
+
|
16
|
+
# publish with rake release:publish
|
17
|
+
namespace :release do
|
18
|
+
desc "Read version, build gem, tag and push release"
|
19
|
+
task :publish do
|
20
|
+
# Read version
|
21
|
+
version_file_path = File.expand_path("../lib/swifter_enum/version.rb", __FILE__)
|
22
|
+
version_file_content = File.read(version_file_path)
|
23
|
+
version_match = version_file_content.match(/VERSION = "(\d+\.\d+\.\d+)"/)
|
24
|
+
unless version_match
|
25
|
+
puts "Version could not be found in #{version_file_path}"
|
26
|
+
exit 1
|
27
|
+
end
|
28
|
+
version = version_match[1]
|
29
|
+
gem_name = "swifter_enum-#{version}.gem"
|
30
|
+
|
31
|
+
# Build gem
|
32
|
+
Rake::Task["build"].invoke
|
33
|
+
|
34
|
+
# Git tag
|
35
|
+
system("git add .")
|
36
|
+
system("git commit -m 'Release version #{version}'")
|
37
|
+
system("git tag -a #{version} -m 'Version #{version} release'")
|
38
|
+
system("git push origin main --tags")
|
39
|
+
|
40
|
+
# Push gem to RubyGems
|
41
|
+
system("gem push pkg/#{gem_name}")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
15
46
|
task default: [:standard, :test]
|
@@ -5,8 +5,9 @@ module SwifterEnum
|
|
5
5
|
|
6
6
|
class_methods do
|
7
7
|
def swifter_enum(enum_name, enum_klass, enum_options = {})
|
8
|
+
|
8
9
|
# Define the enum using values from the enum class
|
9
|
-
enum(enum_name
|
10
|
+
enum(enum_name, enum_klass.values, **enum_options)
|
10
11
|
|
11
12
|
# Define getter method
|
12
13
|
define_method(enum_name) do
|
data/lib/swifter_enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swifter_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob Jonson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-03-
|
11
|
+
date: 2024-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -80,10 +80,24 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '5.22'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: debug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: Simple enum for active record that takes inspiration from Swift's enums
|
84
|
-
to allow you to
|
85
|
-
replacement for regular rails enums, with minimal changes required. Once you
|
86
|
-
then you can easily extend your enums with methods.
|
98
|
+
to allow you to encapsulate enum logic within an enum class. This is easy to drop-in
|
99
|
+
as a replacement for regular rails enums, with minimal changes required. Once you
|
100
|
+
switch, then you can easily extend your enums with methods.
|
87
101
|
email:
|
88
102
|
- rob@hobbyistsoftware.com
|
89
103
|
executables: []
|
@@ -104,7 +118,6 @@ files:
|
|
104
118
|
- lib/swifter_enum/swifter_enum_validator.rb
|
105
119
|
- lib/swifter_enum/version.rb
|
106
120
|
- sig/swifter_enum.rbs
|
107
|
-
- swifter_enum.gemspec
|
108
121
|
homepage: https://github.com/ConfusedVorlon/SwifterEnum
|
109
122
|
licenses:
|
110
123
|
- MIT
|
data/swifter_enum.gemspec
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "lib/swifter_enum/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |spec|
|
6
|
-
spec.name = "swifter_enum"
|
7
|
-
spec.version = SwifterEnum::VERSION
|
8
|
-
spec.authors = ["Rob Jonson"]
|
9
|
-
spec.email = ["rob@hobbyistsoftware.com"]
|
10
|
-
|
11
|
-
spec.summary = "Active Record enum that uses a class, so you can add methods."
|
12
|
-
spec.description = "Simple enum for active record that takes inspiration from Swift's enums to allow you to add logic to your enum definition. This is easy to drop-in as a replacement for regular rails enums, with minimal changes required. Once you switch, then you can easily extend your enums with methods."
|
13
|
-
spec.homepage = "https://github.com/ConfusedVorlon/SwifterEnum"
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
#I'm using 3.2 and above. If you're willing/able to test on lower rubies, then please let me know and feel free to change this.
|
17
|
-
spec.required_ruby_version = ">= 3.2.0"
|
18
|
-
|
19
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
-
spec.metadata["source_code_uri"] = "https://github.com/ConfusedVorlon/SwifterEnum"
|
21
|
-
spec.metadata["changelog_uri"] = "https://github.com/ConfusedVorlon/SwifterEnum"
|
22
|
-
|
23
|
-
# Specify which files should be added to the gem when it is released.
|
24
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
-
spec.files = Dir.chdir(__dir__) do
|
26
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
-
(File.expand_path(f) == __FILE__) ||
|
28
|
-
f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
|
29
|
-
end
|
30
|
-
end
|
31
|
-
spec.bindir = "exe"
|
32
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
33
|
-
spec.require_paths = ["lib"]
|
34
|
-
|
35
|
-
spec.add_dependency "activerecord", "~> 7.0"
|
36
|
-
spec.add_dependency "activesupport", "~> 7.0"
|
37
|
-
spec.add_dependency "activemodel", "~> 7.0"
|
38
|
-
|
39
|
-
# Specify development dependencies
|
40
|
-
spec.add_development_dependency "sqlite3", "~> 1.4" # For using SQL
|
41
|
-
spec.add_development_dependency "minitest", "~> 5.22"
|
42
|
-
|
43
|
-
# For more information and examples about making a new gem, check out our
|
44
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
45
|
-
end
|