swifter_enum 1.0.0 → 1.1.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 +4 -4
- data/CLAUDE.md +46 -0
- data/README.md +25 -1
- data/gemfiles/rails_7.1.gemfile.lock +1 -1
- data/gemfiles/rails_8.0.gemfile.lock +1 -1
- data/lib/swifter_enum/base.rb +12 -0
- data/lib/swifter_enum/version.rb +1 -1
- metadata +2 -2
- data/swifter_enum.gemspec +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b447021b43c633d5d52bdaa9d10175e121eab3ebdefcd8400fb12421c0b127af
|
4
|
+
data.tar.gz: 3d960b5fb93cf81659d049ceba6c40bf397f2f3d051c05e136579738a9f35d2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a4c829b0768b63f2de100eaa8439e066db41b1d8ea6fcf62691121d15c6c16f6b7c70395bd1fd08d34074e05a39fd26326d1dab4f60fdb8d26610047c91d8ea
|
7
|
+
data.tar.gz: 200af325588c59f48568cda24b3125aa420fd6354d66a3c589b58aa879bb37f337fbc09f770d6de321bde9359091affe0130ce201c15215c7ad74bc838aad458
|
data/CLAUDE.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# CLAUDE.md
|
2
|
+
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
4
|
+
|
5
|
+
## Commands
|
6
|
+
|
7
|
+
### Testing
|
8
|
+
- Run all tests: `bundle exec appraisal rake test`
|
9
|
+
- Run specific test file: `bundle exec appraisal rake test TEST=test/path/to/test_file.rb`
|
10
|
+
- Test with specific Rails version: `bundle exec appraisal rails-7.1 rake test`
|
11
|
+
|
12
|
+
### Code Quality
|
13
|
+
- Run Standard Ruby linter: `standardrb`
|
14
|
+
- Auto-fix linting issues: `standardrb --fix`
|
15
|
+
|
16
|
+
### Building & Publishing
|
17
|
+
- Build gem: `rake build`
|
18
|
+
- Release new version: `rake release:publish` (builds, tags, and publishes to RubyGems)
|
19
|
+
|
20
|
+
## Architecture
|
21
|
+
|
22
|
+
SwifterEnum is a Ruby gem that extends Rails ActiveRecord enums by using classes instead of simple string/integer values. This allows encapsulating enum-related logic within the enum class itself.
|
23
|
+
|
24
|
+
### Core Components
|
25
|
+
|
26
|
+
1. **SwifterEnum::Base** (`lib/swifter_enum/base.rb`): Base class for all enum implementations. Key methods:
|
27
|
+
- `set_values`: Defines enum values (accepts hash of symbol->integer or array of symbols/strings)
|
28
|
+
- Instance comparison via `==` supports symbols, strings, and other enum instances
|
29
|
+
- `t` method for i18n translations
|
30
|
+
- `in?` method for collection checking
|
31
|
+
|
32
|
+
2. **SwifterEnum Module** (`lib/swifter_enum/swifter_enum.rb`): ActiveSupport::Concern that adds `swifter_enum` class method to ActiveRecord models. Creates:
|
33
|
+
- Getter returning enum class instance
|
34
|
+
- Setter accepting enum instances or raw values
|
35
|
+
- `_raw` getter/setter for backward compatibility with standard Rails enums
|
36
|
+
- `_raws` class method returning the value mappings
|
37
|
+
|
38
|
+
3. **SwifterEnumValidator** (`lib/swifter_enum/swifter_enum_validator.rb`): Custom validator for enum attributes
|
39
|
+
|
40
|
+
4. **Generator** (`lib/swifter_enum/generators/enum/enum_generator.rb`): Rails generator for creating new enum classes in `app/models/swifter_enum/`
|
41
|
+
|
42
|
+
### Integration Pattern
|
43
|
+
|
44
|
+
Models use `swifter_enum :attribute_name, EnumClass` instead of standard Rails `enum`. This maintains database compatibility while returning enum class instances that can have custom methods.
|
45
|
+
|
46
|
+
The gem maintains full backward compatibility through `_raw` accessors, making it easy to migrate existing Rails enums incrementally.
|
data/README.md
CHANGED
@@ -97,7 +97,31 @@ This provides a richer approach to enums:
|
|
97
97
|
v.camera => #<CameraEnum:0x000000013385f078 @value=:videographer>
|
98
98
|
|
99
99
|
#the purpose of this gem is that you can now define and access methods on the CameraEnum
|
100
|
-
v.camera.icon => "icons/video-camera"
|
100
|
+
v.camera.icon => "icons/video-camera"
|
101
|
+
|
102
|
+
|
103
|
+
### Safe Value Access with Bracket Notation
|
104
|
+
|
105
|
+
You can use bracket notation to safely access enum values, which will raise an error if you try to access a non-existent value:
|
106
|
+
|
107
|
+
# Access enum values with bracket notation
|
108
|
+
emotion = EmotionEnum[:happy]
|
109
|
+
emotion => #<EmotionEnum:0x0000000134c7c290 @value=:happy>
|
110
|
+
|
111
|
+
# Raises ArgumentError for invalid values
|
112
|
+
EmotionEnum[:invalid] # ArgumentError: Unknown enum value: :invalid
|
113
|
+
|
114
|
+
# Must use symbols, not strings
|
115
|
+
EmotionEnum["happy"] # ArgumentError: Enum key must be a Symbol, got String
|
116
|
+
|
117
|
+
This is useful when you want to ensure you're only using valid enum values, such as when processing user input or configuration:
|
118
|
+
|
119
|
+
# Example: Setting a model attribute with validation
|
120
|
+
video.camera = CameraEnum[:videographer] # Safe - will raise if :videographer doesn't exist
|
121
|
+
|
122
|
+
# Example: Using with dynamic values
|
123
|
+
status_key = params[:status].to_sym
|
124
|
+
model.status = StatusEnum[status_key] # Will raise error if status_key is invalid
|
101
125
|
|
102
126
|
|
103
127
|
### Using Enums in ActiveRecord Models
|
data/lib/swifter_enum/base.rb
CHANGED
@@ -19,6 +19,18 @@ module SwifterEnum
|
|
19
19
|
@values.keys.map { |key| new(key) }
|
20
20
|
end
|
21
21
|
|
22
|
+
def [](key)
|
23
|
+
unless key.is_a?(Symbol)
|
24
|
+
raise ArgumentError, "Enum key must be a Symbol, got #{key.class}"
|
25
|
+
end
|
26
|
+
|
27
|
+
unless @values.key?(key)
|
28
|
+
raise ArgumentError, "Unknown enum value: :#{key}. Valid values are: #{@values.keys.map { |k| ":#{k}" }.join(", ")}"
|
29
|
+
end
|
30
|
+
|
31
|
+
new(key)
|
32
|
+
end
|
33
|
+
|
22
34
|
private
|
23
35
|
|
24
36
|
def validate_array_elements!(array)
|
data/lib/swifter_enum/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swifter_enum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob Jonson
|
@@ -138,6 +138,7 @@ files:
|
|
138
138
|
- ".standard.yml"
|
139
139
|
- Appraisals
|
140
140
|
- CHANGELOG.md
|
141
|
+
- CLAUDE.md
|
141
142
|
- LICENSE.txt
|
142
143
|
- README.md
|
143
144
|
- Rakefile
|
@@ -154,7 +155,6 @@ files:
|
|
154
155
|
- lib/swifter_enum/swifter_enum_validator.rb
|
155
156
|
- lib/swifter_enum/version.rb
|
156
157
|
- sig/swifter_enum.rbs
|
157
|
-
- swifter_enum.gemspec
|
158
158
|
homepage: https://github.com/ConfusedVorlon/SwifterEnum
|
159
159
|
licenses:
|
160
160
|
- MIT
|
data/swifter_enum.gemspec
DELETED
@@ -1,47 +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 encapsulate enum logic within an enum class. 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", "< 9.0"
|
36
|
-
spec.add_dependency "activesupport", ">= 7.0", "< 9.0"
|
37
|
-
spec.add_dependency "activemodel", ">= 7.0", "< 9.0"
|
38
|
-
|
39
|
-
# Specify development dependencies
|
40
|
-
spec.add_development_dependency "sqlite3", ">= 1.4"
|
41
|
-
spec.add_development_dependency "minitest", "~> 5.22"
|
42
|
-
spec.add_development_dependency "debug"
|
43
|
-
spec.add_development_dependency "appraisal", "~> 2.4"
|
44
|
-
|
45
|
-
# For more information and examples about making a new gem, check out our
|
46
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
47
|
-
end
|