-
**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 intoActiveSupport::Inflector.underscore
directly, which means that users can bring their own monkey patch if they want to. -
it falls back to
Symbol#downcase
ifString#underscore
is not available. If the constants are defined usingSCREAMING_SNAKE_CASE
, this will generally have the same result as callingString#underscore
, but without requiring ActiveSupport.
class ShippingStatus < Nummy::Enum IN_TRANSIT = auto OUT_FOR_DELIVERY = auto DELIVERED = auto end
# 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}
# 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.class Status < Nummy::Enum ACTIVE = auto ARCHIVED = auto end Status.as_json # => {"ACTIVE" => 0, "ARCHIVED" => 1} Status.to_json # => "{\"ACTIVE\":0,\"ARCHIVED\":1}
-
Add
Nummy::Enum.to_attribute
(#1), which converts the enum to a hash that can be passed as the values of anActiveRecord::Enum
:class Conversation < ActiveRecord::Base class Status < Nummy::Enum ACTIVE = auto ARCHIVED = auto end enum :status, Status.to_attribute end
[!IMPORTANT] The conversion requires that
ActiveRecord::Inflector
is defined, butnummy
does not depend onActiveRecord
directly. If you are usingnummy
in a non-Rails environment, you will need to requireActiveRecord::Inflector
yourself.
- Initial release