nummy 0.1.0 → 0.2.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/CHANGELOG.md +18 -0
- data/README.md +51 -2
- data/lib/nummy/enum.rb +26 -0
- data/lib/nummy/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1eec6bebfcfcfb1a55059d7a9b86bf2f2639a1db72ad9b69294ea3e26bb28d88
|
4
|
+
data.tar.gz: 17d3335c6b7bdfeb12233cf1bca4f95ac11ed608930e0a9fd46a0c92b63ca2fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df883a0535f1b3e2ccf7015d7159d6fd9ba08f6b277324fda3ddf90222e95514f81e792f28fe6994bc1a59f1917bfe0e9cb40fa53b5ab5459172f5d080cd0a20
|
7
|
+
data.tar.gz: 105f1fbf20d88aa5168de81056c072498b7af2d994fbbc203b5317095fba49f6b9d86bc83e9254468c6b49dd4e7338aa0f0d104bbfb21237a3cb5f4af2c1c393
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,23 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.2.0] - 2024-01-04
|
4
|
+
|
5
|
+
- Add `Nummy::Enum.to_attribute`, which converts the enum to a hash that can be passed as the values of an `ActiveRecord::Enum`:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class Conversation < ActiveRecord::Base
|
9
|
+
class Status < Nummy::Enum
|
10
|
+
ACTIVE = auto
|
11
|
+
ARCHIVED = auto
|
12
|
+
end
|
13
|
+
|
14
|
+
enum :status, Status.to_attribute
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
> [!IMPORTANT]
|
19
|
+
> The conversion requires that `ActiveRecord::Inflector` is defined, but `nummy` does not depend on `ActiveRecord` directly. If you are using `nummy` in a non-Rails environment, you will need to require `ActiveRecord::Inflector` yourself.
|
20
|
+
|
3
21
|
## [0.1.0] - 2024-01-04
|
4
22
|
|
5
23
|
- Initial release
|
data/README.md
CHANGED
@@ -14,13 +14,13 @@ constants in a module, and iterating over the members of data classes.
|
|
14
14
|
Install the gem and add to the application's Gemfile by executing:
|
15
15
|
|
16
16
|
```console
|
17
|
-
bundle add
|
17
|
+
bundle add nummy
|
18
18
|
```
|
19
19
|
|
20
20
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
21
21
|
|
22
22
|
```console
|
23
|
-
gem install
|
23
|
+
gem install nummy
|
24
24
|
```
|
25
25
|
|
26
26
|
## Usage
|
@@ -146,6 +146,55 @@ end
|
|
146
146
|
> [!TIP]
|
147
147
|
> `Nummy::Enum` extends `Enumerable` and iterates over the _values_ of the enum, so you have access to things like `.include?(value)`, `.any?`, and `.find`.
|
148
148
|
|
149
|
+
#### Rails / ActiveRecord integration
|
150
|
+
|
151
|
+
> [!IMPORTANT]
|
152
|
+
> This integration requires `ActiveSupport::Inflector` to be defined.
|
153
|
+
|
154
|
+
To use nummy enums as ActiveRecord enums, you can use the `.to_attribute` method:
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
class Conversation < ActiveRecord::Base
|
158
|
+
class Status < Nummy::Enum
|
159
|
+
ACTIVE = auto
|
160
|
+
ARCHIVED = auto
|
161
|
+
end
|
162
|
+
|
163
|
+
enum :status, Status.to_attribute
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
This allows you to use all of the Rails magic for enums, like scopes and boolean helpers, while also being able to refer to values in a safer way than hash lookups.
|
168
|
+
|
169
|
+
That is, these two are the same:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
Conversation.statuses[:active] # => 0
|
173
|
+
Conversation::Status::ACTIVE # => 0
|
174
|
+
```
|
175
|
+
|
176
|
+
But these are not:
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
Conversation.statuses[:acitve]
|
180
|
+
# => nil
|
181
|
+
|
182
|
+
Conversation::Status::ACITVE # => nil
|
183
|
+
# =>
|
184
|
+
# uninitialized constant Conversation::Status::ACITVE (NameError)
|
185
|
+
# Did you mean? Conversation::Status::ACTIVE
|
186
|
+
```
|
187
|
+
|
188
|
+
You can get similar behavior using `#fetch`:
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
Conversation.statuses.fetch(:acitve)
|
192
|
+
# => key not found: :acitve (KeyError)
|
193
|
+
# Did you mean? :active
|
194
|
+
```
|
195
|
+
|
196
|
+
But that still misses out on some of the DX benefits of using constants, like improved support for things like autocompletion, documentation, and navigation ("Go To Definition") in editors.
|
197
|
+
|
149
198
|
### `Nummy::MemberEnumerable`
|
150
199
|
|
151
200
|
`Nummy::MemberEnumerable` is a module that includes `Enumerable` and makes it possible to iterate over any class or module that responds to `members`.
|
data/lib/nummy/enum.rb
CHANGED
@@ -354,6 +354,32 @@ module Nummy
|
|
354
354
|
# Alias to support splatting enums into keyword args.
|
355
355
|
alias to_hash to_h
|
356
356
|
|
357
|
+
# Converts the enum to a +Hash+ with snake_case keys.
|
358
|
+
#
|
359
|
+
# This is intended to be used with +ActiveRecord::Enum+, but can be used
|
360
|
+
# with any library that supports defining enums using a +Hash+ and expects
|
361
|
+
# to have lowercase keys.
|
362
|
+
#
|
363
|
+
# @note
|
364
|
+
# This method _equires +ActiveSupport::Inflector+ to be defined.
|
365
|
+
#
|
366
|
+
# @return [Hash{Symbol => Integer}]
|
367
|
+
#
|
368
|
+
# @example
|
369
|
+
# class Conversation < ActiveRecord::Base
|
370
|
+
# class Status < Nummy::Enum
|
371
|
+
# ACTIVE = auto
|
372
|
+
# ARCHIVED = auto
|
373
|
+
# end
|
374
|
+
#
|
375
|
+
# enum :status, Status.to_attribute
|
376
|
+
# end
|
377
|
+
def to_attribute
|
378
|
+
to_h.transform_keys! do |key|
|
379
|
+
::ActiveSupport::Inflector.underscore(key).to_sym
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
357
383
|
# Returns a string representation of +self+.
|
358
384
|
#
|
359
385
|
# The string will contain the name-value pairs of the constants in +self+.
|
data/lib/nummy/version.rb
CHANGED