Skip to content

Instantly share code, notes, and snippets.

@mnelson
Created November 17, 2011 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnelson/1372650 to your computer and use it in GitHub Desktop.
Save mnelson/1372650 to your computer and use it in GitHub Desktop.
##### USAGE #####
#
# The StaticValue class gives you access to globally accessible constants through a human-readable and compact format.
#
# To define a new set of static values just define a class in this file such as:
# StaticValue.register('MyValues')
#
# Once your class is present you can add values to it via the add_item method:
# MyValues.add_item :Default, 0
# MyValues.add_item :Custom, 1
# MyValues.add_item :UserDefined, 2
#
# Then you access those values like this:
# MyValues::Default, MyValues::Custom
#
# The class gives you a few helpful methods:
# MyValues.all => [0,1,2]
# MyValues.each{|key,value| ... }
# MyValues.to_a => [[:Default, 0], [:Custom, 1], [:UserDefined, 2]]
# MyValues.for_select => [["Default", 0], ["Custom", 1], ["User defined", 2]]
# MyValues.pretty_print(2) => "User defined"
# MyValues.pretty_print(:Default) => 'Default'
# MyValues[:Default] => 0
# MyValues[2] => :UserDefined
#
# Form use case:
# form_for @user do |f|
# f.select :gender, Gender.for_select
# end
#
##################
module StaticValue
UNKNOWN_VALUE = [:Unknown, -1] unless const_defined?('UNKNOWN_VALUE')
class << self
def register(classname)
klazz = Object.const_set(classname.to_s, Class.new do
extend StaticValue::ClassMethods
end) unless Object.const_defined?(classname)
end
end
module ClassMethods
def all
self.to_a.map(&:last)
end
def values
self.all
end
def add_item(key,value)
@hash ||= {}
self.const_set(key, value) unless self.const_defined?(key)
@hash[key]=value
end
def const_missing(key)
@hash[key]
end
def each
to_a.each{|arr| yield(arr[0], arr[1])}
end
def names
to_a.map(&:first)
end
def to_a
@hash.to_a.sort{|a,b| a[1].nil? && 1 || b[1].nil? && -1 || a[1] <=> b[1]}
end
def for_select
sel = []
@hash.values.each do |val|
sel << [pretty_print(val), val]
end
sel.sort{|a,b| a[1].nil? && -1 || b[1].nil? && 1 || a[1] <=> b[1]}
end
def for_select_by_name
for_select.sort{|a,b| a[1].nil? && -1 || b[1].nil? && 1 || a[0] <=> b[0]}
end
def pretty_print(symbol_or_value)
symbol(symbol_or_value).to_s.underscore.humanize
end
def description(symbol_or_value)
I18n.translate("static_value_descriptions.#{self.name.underscore}.#{symbol(symbol_or_value).to_s.underscore}")
end
def [](value)
value = value.to_s
value = value.to_i if value =~ /^\d{1,}$/
if value.is_a?(Fixnum)
matches = @hash.to_a.select{|a| a[1] == value}.first
matches ? matches.first : UNKNOWN_VALUE.first
else
@hash[value.to_sym] || UNKNOWN_VALUE.last
end
end
def symbol(symbol_or_value)
string = ([::Symbol, ::String].include?(symbol_or_value.class) ? symbol_or_value : self[symbol_or_value]).to_s
string.blank? ? UNKNOWN_VALUE.first : string.to_sym
end
def value(symbol_or_value)
self[symbol(symbol_or_value)]
end
end
end
StaticValue.register('Gender')
StaticValue.register('Browser')
Gender.add_item :Male, 0
Gender.add_item :Female, 1
Browser.add_item :Unknown, 0
Browser.add_item :IE9, 1
Browser.add_item :IE8, 2
Browser.add_item :IE7, 3
Browser.add_item :IE6, 4
Browser.add_item :Firefox, 5
Browser.add_item :Safari, 6
Browser.add_item :Chrome, 7
Browser.add_item :Opera, 8
Browser.add_item :Iphone, 9
Browser.add_item :Android, 10
Browser.add_item :Blackberry, 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment