mully (owner)

Revisions

gist: 138612 Download_button fork
public
Public Clone URL: git://gist.github.com/138612.git
MongoDB Rails-Settings.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# This is a generic key/value store model
class AppSetting < MongoRecord::Base
  collection_name :app_settings
    
  #get or set a variable with the variable as the called method
  def self.method_missing(method, *args)
    method_name = method.to_s
    super(method, *args)
    
  rescue NoMethodError
    setting = self.first || self.new
    #set a value for a variable
    if method_name =~ /=$/
      var_name = method_name.gsub('=', '')
      value = args.first
      setting[var_name] = value
      setting.save
    #retrieve a value
    else
      setting[method_name]
    end
  end
  
end