Skip to content

Instantly share code, notes, and snippets.

@dan-watson
Created November 23, 2011 13:25
Show Gist options
  • Select an option

  • Save dan-watson/1388651 to your computer and use it in GitHub Desktop.

Select an option

Save dan-watson/1388651 to your computer and use it in GitHub Desktop.
Key value store for application settings with type casting
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