Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bdchauvette committed Jan 4, 2024
1 parent 303451f commit 2043827
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
64 changes: 63 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,70 @@
## [Unreleased]

## [0.3.0] - 2024-01-04

- **Adds additional flexibility to `Nummy::Enum.to_attribute` (#2)

This release teaches a few new tricks to `Nummy::Enum.to_attribute`:

- it allows passing a block to fully customize how keys are transformed

- 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.

- 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.

```ruby
class ShippingStatus < Nummy::Enum
IN_TRANSIT = auto
OUT_FOR_DELIVERY = auto
DELIVERED = auto
end
```

```ruby
# Without ActiveSupport

ShippingStatus.to_attribute
# v0.2.0 (❌)
# => nummy/lib/nummy/enum.rb:379:in `block in to_attribute': uninitialized constant ActiveSupport (NameError)
#
# ::ActiveSupport::Inflector.underscore(key).to_sym
# ^^^^^^^^^^^^^^^

# v0.3.0 (✅)
# => {:in_transit=>0, :out_for_delivery=>1, :delivered=>2}
```

```ruby
# With ActiveSupport
require "active_support/core_ext/string/inflections"

ShippingStatus.to_attribute
# => {:in_transit=>0, :out_for_delivery=>1, :delivered=>2}

ShippingStatus.to_attribute { |key| key.to_s.underscore.camelize.to_sym }
# => {:InTransit=>0, :OutForDelivery=>1, :Delivered=>2}
```

- **Adds JSON serialization support (#3)**

This release adds two methods to `Nummy::Enum`: `.as_json` and `.to_json`. These methods allow you to serialize an enum to JSON.

```ruby
class Status < Nummy::Enum
ACTIVE = auto
ARCHIVED = auto
end

Status.as_json
# => {"ACTIVE" => 0, "ARCHIVED" => 1}

Status.to_json
# => "{\"ACTIVE\":0,\"ARCHIVED\":1}
```

## [0.2.0] - 2024-01-04

- Add `Nummy::Enum.to_attribute`, which converts the enum to a hash that can be passed as the values of an `ActiveRecord::Enum`:
- Add `Nummy::Enum.to_attribute` (#1), which converts the enum to a hash that can be passed as the values of an `ActiveRecord::Enum`:

```ruby
class Conversation < ActiveRecord::Base
Expand Down
2 changes: 1 addition & 1 deletion lib/nummy/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Nummy
# The current version of the gem.
VERSION = "0.2.0"
VERSION = "0.3.0"
end

0 comments on commit 2043827

Please sign in to comment.