nummy 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1eec6bebfcfcfb1a55059d7a9b86bf2f2639a1db72ad9b69294ea3e26bb28d88
4
- data.tar.gz: 17d3335c6b7bdfeb12233cf1bca4f95ac11ed608930e0a9fd46a0c92b63ca2fa
3
+ metadata.gz: 83fe6928d06ebd4ba649eaa37ad3278366e2322a99fe56a7efdfed228b971841
4
+ data.tar.gz: 7116e7d5febca95ae52544bcbc42a4fd99e895ca8e60ef9cd7e3444e6dae8719
5
5
  SHA512:
6
- metadata.gz: df883a0535f1b3e2ccf7015d7159d6fd9ba08f6b277324fda3ddf90222e95514f81e792f28fe6994bc1a59f1917bfe0e9cb40fa53b5ab5459172f5d080cd0a20
7
- data.tar.gz: 105f1fbf20d88aa5168de81056c072498b7af2d994fbbc203b5317095fba49f6b9d86bc83e9254468c6b49dd4e7338aa0f0d104bbfb21237a3cb5f4af2c1c393
6
+ metadata.gz: 4d4bb886ad7d5187aca88391c1e9366a82b4bdb08e435cb5567951a5e2a5e5d230ef5f06cdae0bd5b104c8a62d00e41c437969cf9b9b4a4cf7d5eb41e73dd4ac
7
+ data.tar.gz: dc40fbde79187708d1006bd80475b114f98f8de238faaae8bd4a9b620f5557df06c3d960123e237267041370f59baee13d42e1f4389e97e3394e68fe6e383cfc
data/CHANGELOG.md CHANGED
@@ -1,8 +1,70 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2024-01-04
4
+
5
+ - **Adds additional flexibility to `Nummy::Enum.to_attribute` (#2)
6
+
7
+ This release teaches a few new tricks to `Nummy::Enum.to_attribute`:
8
+
9
+ - it allows passing a block to fully customize how keys are transformed
10
+
11
+ - it looks for `String#underscore` rather than calling into `ActiveSupport::Inflector.underscore` directly, which means that users can bring their own monkey patch if they want to.
12
+
13
+ - it falls back to `Symbol#downcase` if `String#underscore` is not available. If the constants are defined using `SCREAMING_SNAKE_CASE`, this will generally have the same result as calling `String#underscore`, but without requiring ActiveSupport.
14
+
15
+ ```ruby
16
+ class ShippingStatus < Nummy::Enum
17
+ IN_TRANSIT = auto
18
+ OUT_FOR_DELIVERY = auto
19
+ DELIVERED = auto
20
+ end
21
+ ```
22
+
23
+ ```ruby
24
+ # Without ActiveSupport
25
+
26
+ ShippingStatus.to_attribute
27
+ # v0.2.0 (❌)
28
+ # => nummy/lib/nummy/enum.rb:379:in `block in to_attribute': uninitialized constant ActiveSupport (NameError)
29
+ #
30
+ # ::ActiveSupport::Inflector.underscore(key).to_sym
31
+ # ^^^^^^^^^^^^^^^
32
+
33
+ # v0.3.0 (✅)
34
+ # => {:in_transit=>0, :out_for_delivery=>1, :delivered=>2}
35
+ ```
36
+
37
+ ```ruby
38
+ # With ActiveSupport
39
+ require "active_support/core_ext/string/inflections"
40
+
41
+ ShippingStatus.to_attribute
42
+ # => {:in_transit=>0, :out_for_delivery=>1, :delivered=>2}
43
+
44
+ ShippingStatus.to_attribute { |key| key.to_s.underscore.camelize.to_sym }
45
+ # => {:InTransit=>0, :OutForDelivery=>1, :Delivered=>2}
46
+ ```
47
+
48
+ - **Adds JSON serialization support (#3)**
49
+
50
+ This release adds two methods to `Nummy::Enum`: `.as_json` and `.to_json`. These methods allow you to serialize an enum to JSON.
51
+
52
+ ```ruby
53
+ class Status < Nummy::Enum
54
+ ACTIVE = auto
55
+ ARCHIVED = auto
56
+ end
57
+
58
+ Status.as_json
59
+ # => {"ACTIVE" => 0, "ARCHIVED" => 1}
60
+
61
+ Status.to_json
62
+ # => "{\"ACTIVE\":0,\"ARCHIVED\":1}
63
+ ```
64
+
3
65
  ## [0.2.0] - 2024-01-04
4
66
 
5
- - Add `Nummy::Enum.to_attribute`, which converts the enum to a hash that can be passed as the values of an `ActiveRecord::Enum`:
67
+ - Add `Nummy::Enum.to_attribute` (#1), which converts the enum to a hash that can be passed as the values of an `ActiveRecord::Enum`:
6
68
 
7
69
  ```ruby
8
70
  class Conversation < ActiveRecord::Base
data/README.md CHANGED
@@ -148,10 +148,7 @@ end
148
148
 
149
149
  #### Rails / ActiveRecord integration
150
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:
151
+ To use nummy enums as ActiveRecord enums, you can use the `.to_attribute` method, which converts the keys to `snake_case` by default:
155
152
 
156
153
  ```ruby
157
154
  class Conversation < ActiveRecord::Base
@@ -164,7 +161,13 @@ class Conversation < ActiveRecord::Base
164
161
  end
165
162
  ```
166
163
 
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.
164
+ > [!NOTE]
165
+ > `to_attribute` will transform keys using `String#underscore` if it is defined, otherwise it will use `Symbol#downcase`.
166
+
167
+ > [!TIP]
168
+ > You can also do custom transformations by passing a block to `to_attribute`. See the documentation for more details.
169
+
170
+ Using `to_attribute` 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
171
 
169
172
  That is, these two are the same:
170
173
 
data/lib/nummy/enum.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
4
+
3
5
  require "nummy/auto_sequenceable"
4
6
  require "nummy/ordered_const_enumerable"
5
7
  require "nummy/errors"
@@ -354,30 +356,77 @@ module Nummy
354
356
  # Alias to support splatting enums into keyword args.
355
357
  alias to_hash to_h
356
358
 
359
+ # Converts +self+ to a +Hash+ that can be converted to JSON.
360
+ #
361
+ # @return [Hash{String => Object}]
362
+ #
363
+ # @see .to_json
364
+ def as_json
365
+ to_h.transform_keys!(&:to_s)
366
+ end
367
+
368
+ # Converts +self+ to a +Hash+ and serializes it to a JSON string.
369
+ #
370
+ # Supports same options as +JSON#generate+.
371
+ #
372
+ # @return [String]
373
+ #
374
+ # @see .as_json
375
+ def to_json(*)
376
+ as_json.to_json(*)
377
+ end
378
+
357
379
  # Converts the enum to a +Hash+ with snake_case keys.
358
380
  #
359
381
  # This is intended to be used with +ActiveRecord::Enum+, but can be used
360
382
  # with any library that supports defining enums using a +Hash+ and expects
361
383
  # to have lowercase keys.
362
384
  #
363
- # @note
364
- # This method _equires +ActiveSupport::Inflector+ to be defined.
385
+ # @overload to_attribute
386
+ # If +String#underscore+ is defined, converts the keys to snake_case
387
+ # by calling +underscore+ on the stringified key.
388
+ #
389
+ # Otherwise, converts each key using +Symbol#downcase+.
390
+ #
391
+ # @return [Hash{Symbol => Object}]
365
392
  #
366
- # @return [Hash{Symbol => Integer}]
393
+ # @example Using with +ActiveRecord::Enum+.
394
+ # class Conversation < ActiveRecord::Base
395
+ # class Status < Nummy::Enum
396
+ # ACTIVE = auto
397
+ # ARCHIVED = auto
398
+ # end
367
399
  #
368
- # @example
369
- # class Conversation < ActiveRecord::Base
370
- # class Status < Nummy::Enum
371
- # ACTIVE = auto
372
- # ARCHIVED = auto
400
+ # enum :status, Status.to_attribute
373
401
  # end
374
402
  #
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
403
+ # @overload to_attribute(&)
404
+ # Transforms the keys by calling the given block on each key.
405
+ #
406
+ # @yieldparam key [Symbol]
407
+ # @return [Hash{Symbol => Object}]
408
+ #
409
+ # @example Using custom key transformer.
410
+ # class ShippingStatus < Nummy::Enum
411
+ # IN_TRANSIT = auto
412
+ # OUT_FOR_DELIVERY = auto
413
+ # DELIVERED = auto
414
+ # end
415
+ #
416
+ # ShippingStatus.to_attribute { |key| key.to_s.underscore.camelize.to_sym }
417
+ # # => {:InTransit=>0, :OutForDelivery=>1, :Delivered=>2}
418
+ def to_attribute(&)
419
+ return to_h.transform_keys!(&) if block_given?
420
+
421
+ # Ignore coverage because it's executed in a forked process, which
422
+ # messes with the coverage collection.
423
+ # :nocov:
424
+ if String.method_defined?(:underscore)
425
+ return to_attribute { |key| key.to_s.underscore.to_sym }
380
426
  end
427
+ # :nocov:
428
+
429
+ to_attribute(&:downcase)
381
430
  end
382
431
 
383
432
  # Returns a string representation of +self+.
data/lib/nummy/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Nummy
4
4
  # The current version of the gem.
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nummy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Chauvette