Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions lib/power_enum/enumerated.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -79,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

Expand Down Expand Up @@ -134,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
Expand Down Expand Up @@ -193,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.
Expand Down Expand Up @@ -445,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
Expand All @@ -468,15 +474,15 @@ 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

# 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'
Expand Down
10 changes: 10 additions & 0 deletions spec/functional/acts_as_enumerated_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down