Sha256: bcdacc584202229327c4326ad363d65bd8aa265f5f1c58ae7b20ff9545ae4f7b
Contents?: true
Size: 1.24 KB
Versions: 1
Compression:
Stored size: 1.24 KB
Contents
require 'i18n' require 'active_support/inflector' module EZEnum class << self def included(base) const_names = base.constants(false) const_names_by_value = const_names.inject({}) do |list, name| list[base.const_get(name)] = name list end # add an "All" constant base.const_set :All, const_names_by_value.keys # add helper methods to module base.class_eval do # provides an array of display and values pairs # typcially for use in HTML select controls define_singleton_method :choices_for_select do const_names_by_value.collect do |value, name| [translate_display_for(name), value] end end # resolves for the display name for the given value define_singleton_method :display_for do |value| translate_display_for(const_names_by_value[value]) end private # lookup i18n translation for given name define_singleton_method :translate_display_for do |const_name| I18n.translate( const_name.to_s.underscore.to_sym, :scope => [:enums, base.name.underscore], :default => const_name.to_s ) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ez_enum-0.0.1 | lib/ez_enum.rb |