Created
November 23, 2011 13:25
-
-
Save dan-watson/1388651 to your computer and use it in GitHub Desktop.
Key value store for application settings with type casting
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 Application < ActiveRecord::Base | |
| set_table_name "application_settings" | |
| col :key, :limit => 50 | |
| col :value, :limit => 100 | |
| def self.method_missing(method, *args, &block) | |
| return self.send method, *args, &block if self.respond_to? method | |
| if method.to_s =~ /=/ | |
| return self.set method.to_s.gsub("=", ""), args.first | |
| else | |
| return self.get method.to_s | |
| end | |
| end | |
| private | |
| def self.get setting | |
| entry = Application.where(:key => setting).first | |
| YAML.load(entry.value) | |
| end | |
| def self.set key, value | |
| setting = Application.where(:key => key).first || Application.new(:key => key) | |
| setting.update_attribute(:value, value.to_yaml) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment