From d14d6b5e7744e93ab2fadfe17d87a65405a4d1d3 Mon Sep 17 00:00:00 2001 From: Kurt Stephens Date: Fri, 13 Jun 2025 09:43:17 -0500 Subject: [PATCH 1/7] frozen_string_literal: true. --- lib/power_enum/enumerated.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/power_enum/enumerated.rb b/lib/power_enum/enumerated.rb index ed8afc2..680a5c6 100644 --- a/lib/power_enum/enumerated.rb +++ b/lib/power_enum/enumerated.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # Copyright (c) 2005 Trevor Squires # Copyright (c) 2012 Arthur Shagall # Released under the MIT License. See the LICENSE file for more details. From fe48824177a2330073221aaf1e0047ac31c70af7 Mon Sep 17 00:00:00 2001 From: Kurt Stephens Date: Fri, 13 Jun 2025 09:43:53 -0500 Subject: [PATCH 2/7] #to_s is implied in String interpolation. --- lib/power_enum/enumerated.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/power_enum/enumerated.rb b/lib/power_enum/enumerated.rb index 680a5c6..a5d1c0c 100644 --- a/lib/power_enum/enumerated.rb +++ b/lib/power_enum/enumerated.rb @@ -81,9 +81,9 @@ def acts_as_enumerated(options = {}) options.assert_valid_keys(*valid_keys) valid_keys.each do |key| - class_attribute "acts_enumerated_#{key.to_s}" + class_attribute "acts_enumerated_#{key}" if options.has_key?( key ) - self.send "acts_enumerated_#{key.to_s}=", options[key] + self.send "acts_enumerated_#{key}=", options[key] end end From f91b97726a19245a51845e39826104dc3684f878 Mon Sep 17 00:00:00 2001 From: Kurt Stephens Date: Fri, 13 Jun 2025 09:45:53 -0500 Subject: [PATCH 3/7] #__enum_name__: cache and freeze String. --- lib/power_enum/enumerated.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/power_enum/enumerated.rb b/lib/power_enum/enumerated.rb index a5d1c0c..082a3ad 100644 --- a/lib/power_enum/enumerated.rb +++ b/lib/power_enum/enumerated.rb @@ -136,9 +136,11 @@ def extend_enum_class_methods(options) #:nodoc: validate :validate_enumeration_model_updates_permitted define_method :__enum_name__ do - read_attribute(acts_enumerated_name_column).to_s + @__enum_name__ ||= read_attribute(acts_enumerated_name_column).dup.to_s.freeze end + alias_method :to_s, :__enum_name__ + if should_alias_name?(options) && acts_enumerated_name_column != :name alias_method :name, :__enum_name__ end @@ -476,9 +478,9 @@ def name_sym alias_method :to_sym, :name_sym # By default enumeration #to_s should return stringified name of the enum. BookingStatus[:foo].to_s returns "foo" - def to_s - self.__enum_name__ - end + # def to_s + # __enum_name__ + # end # Returns true if the instance is active, false otherwise. If it has an attribute 'active', # returns the attribute cast to a boolean, otherwise returns true. This method is used by the 'active' From 9e3e0b27b41f31962bdaf8180166c89c5db0015a Mon Sep 17 00:00:00 2001 From: Kurt Stephens Date: Fri, 13 Jun 2025 09:47:54 -0500 Subject: [PATCH 4/7] #name_sym: caching is ~ 15% faster. --- lib/power_enum/enumerated.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/power_enum/enumerated.rb b/lib/power_enum/enumerated.rb index 082a3ad..c53d64b 100644 --- a/lib/power_enum/enumerated.rb +++ b/lib/power_enum/enumerated.rb @@ -472,7 +472,7 @@ def in?(*list) # Returns the symbol representation of the name of the enum. BookingStatus[:foo].name_sym returns :foo. def name_sym - self.__enum_name__.to_sym + @name_sym ||= __enum_name__.to_sym end alias_method :to_sym, :name_sym From a5f6b55295bad226cdd83797196c480c2f7f166f Mon Sep 17 00:00:00 2001 From: Kurt Stephens Date: Fri, 13 Jun 2025 09:53:26 -0500 Subject: [PATCH 5/7] Use @ivar ||= foo pattern. --- lib/power_enum/enumerated.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/power_enum/enumerated.rb b/lib/power_enum/enumerated.rb index c53d64b..6107036 100644 --- a/lib/power_enum/enumerated.rb +++ b/lib/power_enum/enumerated.rb @@ -197,14 +197,12 @@ def all # Returns all the active enum values. See the 'active?' instance method. def active - return @all_active if @all_active - @all_active = all.find_all{ |enum| enum.active? }.freeze + @all_active ||= all.find_all{ |enum| enum.active? }.freeze end # Returns all the inactive enum values. See the 'inactive?' instance method. def inactive - return @all_inactive if @all_inactive - @all_inactive = all.find_all{ |enum| !enum.active? }.freeze + @all_inactive ||= all.find_all{ |enum| !enum.active? }.freeze end # Returns the names of all the enum values as an array of symbols. From cb72dfaf3bb5fa32159a8da686c41723577e63a8 Mon Sep 17 00:00:00 2001 From: Kurt Stephens Date: Fri, 13 Jun 2025 09:54:16 -0500 Subject: [PATCH 6/7] #===: simplify logic. --- lib/power_enum/enumerated.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/power_enum/enumerated.rb b/lib/power_enum/enumerated.rb index 6107036..dcff541 100644 --- a/lib/power_enum/enumerated.rb +++ b/lib/power_enum/enumerated.rb @@ -447,12 +447,16 @@ module EnumInstanceMethods # also raise an exception for any lookup failure of +BookingStatus[arg]+. def ===(arg) case arg + when Symbol + to_sym == arg + when Integer + id == arg + when String + __enum_name__ == arg when nil false - when Symbol, String, Integer - return self == self.class[arg] when Array - return self.in?(*arg) + self.in?(*arg) else super end From e302d976feb392bf01d3688af7fcfc1f38962c95 Mon Sep 17 00:00:00 2001 From: Kurt Stephens Date: Fri, 13 Jun 2025 09:54:43 -0500 Subject: [PATCH 7/7] spec: assert #__enum_name__ returns the same frozen String. --- spec/functional/acts_as_enumerated_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spec/functional/acts_as_enumerated_spec.rb b/spec/functional/acts_as_enumerated_spec.rb index 567ef16..b8b1cd2 100644 --- a/spec/functional/acts_as_enumerated_spec.rb +++ b/spec/functional/acts_as_enumerated_spec.rb @@ -78,6 +78,16 @@ def flush_cache(klass) end end + describe '#__enum_name__' do + let(:enum) { BookingStatus['confirmed'] } + it "returns a frozen String" do + enum.__enum_name__.frozen?.should == true + end + it "returns the same String object" do + enum.__enum_name__.object_id.should == enum.__enum_name__.object_id + end + end + describe '[]' do context 'record exists' do