Created
April 23, 2012 02:11
-
-
Save augustf/2468275 to your computer and use it in GitHub Desktop.
Config key-value store\
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Configuration < ActiveRecord::Base | |
TRUE = "t" | |
FALSE = "f" | |
validates_presence_of :key | |
validates_uniqueness_of :key | |
# Enable hash-like access to table for ease of use | |
# Returns false if key isn't found | |
# Example: Configuration[:public_concerto] => 80 | |
def self.[](key) | |
rec = self.find_by_key(key.to_s) | |
if rec.nil? | |
return false | |
end | |
rec.value | |
end | |
# Override self.method_missing to allow | |
# instance attribute type access to Configuration | |
# table. This helps with forms. | |
def self.method_missing(method, *args) | |
unless method.to_s.include?('find') # skip AR find methods | |
value = self[method] | |
return value unless value.nil? | |
end | |
super(method, args) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment