lib/simple_enum.rb in simple_enum-1.2.0 vs lib/simple_enum.rb in simple_enum-1.3.0

- old
+ new

@@ -19,11 +19,11 @@ # Base module which gets included in <tt>ActiveRecord::Base</tt>. See documentation # of +SimpleEnum::ClassMethods+ for more details. module SimpleEnum # +SimpleEnum+ version string. - VERSION = "1.2.0".freeze + VERSION = "1.3.0".freeze class << self # Provides configurability to SimpleEnum, allows to override some defaults which are # defined for all uses of +as_enum+. Most options from +as_enum+ are available, such as: @@ -174,28 +174,27 @@ raise(ArgumentError, "Invalid enumeration value: #{new_value}") if (options[:whiny] and v.nil? and !new_value.blank?) write_attribute options[:column], v end # allow access to defined values hash, e.g. in a select helper or finder method. - self_name = enum_cd.to_s.pluralize - self_name.upcase! if options[:upcase] + attr_name = enum_cd.to_s.pluralize + enum_attr = :"#{attr_name.downcase}_enum_hash" + write_inheritable_attribute(enum_attr, values) - class_eval(<<-EOM, __FILE__, __LINE__ + 1) - @#{self_name} = values - - def self.#{self_name}(*args) - return @#{self_name} if args.first.nil? - return @#{self_name}[args.first] if args.size == 1 - args.inject([]) { |ary, sym| ary << @#{self_name}[sym]; ary } + class_eval(<<-RUBY, __FILE__, __LINE__ + 1) + def self.#{attr_name}(*args) + return read_inheritable_attribute(#{enum_attr.inspect}) if args.first.nil? + return read_inheritable_attribute(#{enum_attr.inspect})[args.first] if args.size == 1 + args.inject([]) { |ary, sym| ary << read_inheritable_attribute(#{enum_attr.inspect})[sym]; ary } end - def self.#{self_name}_for_select(&block) - self.#{self_name}.map do |k,v| - [block_given? ? yield(k,v) : self.human_enum_name(#{self_name.inspect}, k), k] + def self.#{attr_name}_for_select(&block) + self.#{attr_name}.map do |k,v| + [block_given? ? yield(k,v) : self.human_enum_name(#{attr_name.inspect}, k), k] end.sort end - EOM + RUBY # only create if :slim is not defined if options[:slim] != true # create both, boolean operations and *bang* operations for each # enum "value" @@ -212,14 +211,10 @@ sym end # allow class access to each value unless options[:slim] === :class - if self.respond_to? :singleton_class - singleton_class.send(:define_method, "#{prefix}#{sym}", Proc.new { |*args| args.first ? k : code }) - else - metaclass.send(:define_method, "#{prefix}#{sym}", Proc.new { |*args| args.first ? k : code }) - end + singleton_class.send(:define_method, "#{prefix}#{sym}", Proc.new { |*args| args.first ? k : code }) end end end end