swifter_enum 0.9.7 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a44ef43342b61052d8fafbf4d708c6f961f357b79fcae2d0a823e2798e366f09
4
- data.tar.gz: 9e18b6bd910d99df69be802da1a59b98830acbbd3e36a33cbf0fc82c134de7a5
3
+ metadata.gz: b447021b43c633d5d52bdaa9d10175e121eab3ebdefcd8400fb12421c0b127af
4
+ data.tar.gz: 3d960b5fb93cf81659d049ceba6c40bf397f2f3d051c05e136579738a9f35d2b
5
5
  SHA512:
6
- metadata.gz: b475b348d21d7de7b04e4f24e3ab8ba57ac4fa6f9d95c2996fe3d3506666a93140e3403ebbb6ea9635dee0496b15f64cb41d43ee50e5b603b07d7f11cf5343f9
7
- data.tar.gz: 2992a30aefa8d496ede7cdc4f788004e826b19a40081e11f7e18af64073596fce68a719187955d464b929c190f1b2c4c094c569ecdfab2fdaa0dc204cc4b6557
6
+ metadata.gz: 1a4c829b0768b63f2de100eaa8439e066db41b1d8ea6fcf62691121d15c6c16f6b7c70395bd1fd08d34074e05a39fd26326d1dab4f60fdb8d26610047c91d8ea
7
+ data.tar.gz: 200af325588c59f48568cda24b3125aa420fd6354d66a3c589b58aa879bb37f337fbc09f770d6de321bde9359091affe0130ce201c15215c7ad74bc838aad458
data/Appraisals CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  appraise "rails-7.1" do
3
2
  gem "activerecord", "~> 7.1.0"
4
3
  gem "activesupport", "~> 7.1.0"
@@ -11,4 +10,4 @@ appraise "rails-8.0" do
11
10
  gem "activesupport", "~> 8.0.0"
12
11
  gem "activemodel", "~> 8.0.0"
13
12
  gem "sqlite3", ">= 2.1" # Rails 8 requires sqlite3 2.1+
14
- end
13
+ end
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- swifter_enum (0.9.6)
4
+ swifter_enum (1.0.0)
5
5
  activemodel (>= 7.0, < 9.0)
6
6
  activerecord (>= 7.0, < 9.0)
7
7
  activesupport (>= 7.0, < 9.0)
@@ -49,6 +49,7 @@ GEM
49
49
  json (2.10.2)
50
50
  language_server-protocol (3.17.0.4)
51
51
  lint_roller (1.1.0)
52
+ mini_portile2 (2.8.9)
52
53
  minitest (5.25.5)
53
54
  mutex_m (0.3.0)
54
55
  parallel (1.26.3)
@@ -87,7 +88,8 @@ GEM
87
88
  rubocop (>= 1.48.1, < 2.0)
88
89
  rubocop-ast (>= 1.31.1, < 2.0)
89
90
  ruby-progressbar (1.13.0)
90
- sqlite3 (1.7.2-arm64-darwin)
91
+ sqlite3 (1.7.2)
92
+ mini_portile2 (~> 2.8.0)
91
93
  standard (1.40.0)
92
94
  language_server-protocol (~> 3.17.0.2)
93
95
  lint_roller (~> 1.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- swifter_enum (0.9.6)
4
+ swifter_enum (1.0.0)
5
5
  activemodel (>= 7.0, < 9.0)
6
6
  activerecord (>= 7.0, < 9.0)
7
7
  activesupport (>= 7.0, < 9.0)
@@ -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)
@@ -42,6 +54,10 @@ module SwifterEnum
42
54
  end
43
55
  end
44
56
 
57
+ def in?(collection)
58
+ collection.any? { |item| self == item }
59
+ end
60
+
45
61
  def t
46
62
  I18n.t("swifter_enum.#{self.class.name.demodulize.underscore}.#{value}")
47
63
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SwifterEnum
4
- VERSION = "0.9.7"
4
+ VERSION = "1.1.0"
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swifter_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Jonson
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-04-12 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activerecord
@@ -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
@@ -176,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
176
  - !ruby/object:Gem::Version
177
177
  version: '0'
178
178
  requirements: []
179
- rubygems_version: 3.6.4
179
+ rubygems_version: 3.6.8
180
180
  specification_version: 4
181
181
  summary: Active Record enum that uses a class, so you can add methods.
182
182
  test_files: []
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