lib/simple_enum.rb in simple_enum-1.6.2 vs lib/simple_enum.rb in simple_enum-1.6.3
- old
+ new
@@ -35,10 +35,11 @@
# * <tt>:whiny</tt> - Boolean value which if set to <tt>true</tt> will throw an <tt>ArgumentError</tt>
# if an invalid value is passed to the setter (e.g. a value for which no enumeration exists). if set to
# <tt>false</tt> no exception is thrown and the internal value is set to <tt>nil</tt> (default is <tt>true</tt>)
# * <tt>:dirty</tt> - Boolean value which if set to <tt>true</tt> generates <tt>..._was</tt> and <tt>..._changed?</tt>
# methods for the enum, which delegate to the internal column.
+ # * <tt>:strings</tt> - Boolean value which if set to <tt>true</tt> defaults array values as strings instead of integers.
def default_options
@default_options ||= {
:whiny => true,
:upcase => false
}
@@ -152,19 +153,20 @@
# * <tt>:whiny</tt> - Boolean value which if set to <tt>true</tt> will throw an <tt>ArgumentError</tt>
# if an invalid value is passed to the setter (e.g. a value for which no enumeration exists). if set to
# <tt>false</tt> no exception is thrown and the internal value is set to <tt>nil</tt> (default is <tt>true</tt>)
# * <tt>:dirty</tt> - Boolean value which if set to <tt>true</tt> generates <tt>..._was</tt> and <tt>..._changed?</tt>
# methods for the enum, which delegate to the internal column (default is <tt>false</tt>)
+ # * <tt>:strings</tt> - Boolean value which if set to <tt>true</tt> stores array values as strings instead of it's index.
# * <tt>:field</tt> - Also allowed as valid key, for Mongoid integration + default options, see simple_enum#27.
#
def as_enum(enum_cd, values, options = {})
options = SimpleEnum.default_options.merge({ :column => "#{enum_cd}_cd" }).merge(options)
- options.assert_valid_keys(:column, :whiny, :prefix, :slim, :upcase, :dirty, :field)
+ options.assert_valid_keys(:column, :whiny, :prefix, :slim, :upcase, :dirty, :strings, :field)
metaclass = (class << self; self; end)
- # convert array to hash...
- values = SimpleEnum::EnumHash.new(values)
+ # convert array to hash
+ values = SimpleEnum::EnumHash.new(values, options[:strings])
values_inverted = values.invert
# store info away
self.enum_definitions[enum_cd] = self.enum_definitions[options[:column]] = { :name => enum_cd, :column => options[:column], :options => options }