public
Created

  • Download Gist
static_value.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
##### 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.