lib/riveter/enumerated.rb in riveter-0.0.4 vs lib/riveter/enumerated.rb in riveter-0.0.5

- old
+ new

@@ -2,31 +2,36 @@ module Enumerated extend ActiveSupport::Concern included do - const_names = self.constants(false) - const_values = const_names.collect {|name| self.const_get(name) } + const_names = self.constants(false).freeze + const_values = const_names.collect {|name| self.const_get(name) }.freeze # create hashes for decoding names and values const_value_lookup ||= const_names.inject({}) {|list, name| list[name] = self.const_get(name) list - }.with_indifferent_access + }.with_indifferent_access.freeze const_name_lookup ||= const_names.inject({}) {|list, name| list[self.const_get(name)] = name list - } + }.freeze # define helper methods # returns an array of the constant names as symbols define_singleton_method :names do const_names end + # returns an array of the constant names as symbols + define_singleton_method :values do + const_values + end + # returns an array of constant values define_singleton_method :all do const_values end @@ -62,10 +67,10 @@ # over the set of constants and provides some helper methods self.const_set :All, Enumeration.new(self, const_values) # for use within form select inputs define_singleton_method :collection do - self.const_get(:All).collect {|m| [m.human, m.value] } + self.const_get(:All).collect {|m| [m.human, m.name] } end end module ClassMethods def human(options={})