ActiveRecord - Simple Key Value Store

Over the last few weeks I have been playing with the idea of having a simple database backed key value store using Rails ActiveRecord. The ActiveRecord class should act like a singleton where arbitrary keys can be set with a value of any type (simple values, hashes, arrays or complex objects). I have come up with the idea of using method missing to set and retrieve the key / value pair where the value's are saved as yaml and then cast back to the correct type when retrieved.

            class Application < ActiveRecord::Base
              set_table_name "application_settings"
              
              def self.method_missing(method, *args, &block)
                return self.send method, *args, &block if self.respond_to? method
                method_name = method.to_s
                if method_name =~ /=/
                  return self.set method_name.gsub("=", ""), args.first
                else
                  return self.get method_name
                end
              end
              
              private 
              def self.get setting
                entry = Application.where(:key => setting).first
                entry.nil? ? nil : 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
            

Below is the migration needed to setup the database table for your application settings class.

            class CreateApplicationSettings < ActiveRecord::Migration
              def up
                create_table :application_settings, :id => false do |t|
                  t.string :key
                  t.text :value
            
                  t.timestamps
                end
                add_index :application_settings, :key, :unique => true
              end
              
              def down
                drop_table :application_settings
              end
            end
            

Using the class it is possible to set a value to any arbitrary key by calling the setting name setter method just like it is a defined class attribute.

            Application.some_arbitrary_setting = 12903
            Application.my_hash_foo = { :foo => "bar" }
            

To retrieve the setting make a method call to the key name as if it were a defined class attribute.

            Application.some_arbitrary_setting
            => 12903 
            
            Application.my_hash_foo 
            => {:foo=>"bar"}
            
Tweet Me | Link To Facebook
Tom
Sunday, 4th December, 2011
spacer
You lose dynamic finders (find_by_...) by overriding method missing and not calling super, though you could argue that in this case they are no longer required.
Other than that this is a neat solution.
Tom
Sunday, 4th December, 2011
spacer
I think you shouldn't make get and set private, right now if the user wanted to set some keys that are determined programatically he'd have to resort to using send, that's a little ugly.
Dan Watson
Sunday, 4th December, 2011
spacer
I do think losing find_by_? in this case is fine. Also making get and set public would be a better idea for sure!

Month List

  • 2011-December (3)
  • 2011-November (2)
  • 2011-October (1)
  • 2011-September (1)
  • 2011-August (1)
  • 2011-June (2)
  • 2011-May (4)
  • 2011-April (1)
  • 2011-March (3)
  • 2011-February (2)
  • 2011-January (2)
  • 2010-December (1)
  • 2010-November (1)
  • 2010-September (1)
  • 2010-June (3)
  • 2010-May (1)
  • 2010-April (3)
  • 2010-March (2)
  • 2010-February (2)
  • 2010-January (2)
  • 2009-December (2)
  • 2009-November (2)
  • 2009-October (2)
  • 2009-September (2)
  • 2009-July (2)
  • 2009-June (2)
  • 2009-May (2)
  • 2009-April (2)

Tag Cloud

  • activerecord
  • annotate
  • appsettings
  • ashx
  • asp.net
  • asp.net mvc
  • asp.netmvc
  • blogging
  • book review
  • bookreview
  • c#
  • caching
  • calender
  • cancer
  • data annotations
  • datamapper
  • dependency injection
  • development
  • errors
  • fluent nhibernate
  • funny
  • hardware
  • http
  • httpruntime
  • iis7
  • image resize
  • irb
  • jacob
  • javascript
  • jquery
  • jruby
  • jsshell
  • kue
  • life
  • linq 2 nhibernate
  • linq 2 sql
  • master pages
  • mini_record
  • model
  • mvc
  • mysql
  • nhibernate
  • nunit
  • patterns
  • performance testing
  • powershell
  • productivity
  • pry
  • rapuncel
  • ravendb
  • ruby
  • ruby on rails
  • rubyonrails
  • rvm
  • security
  • selectlist
  • session
  • shout mouth
  • specs
  • sqlite
  • stored procedures
  • structuremap
  • theming
  • tools
  • tribe.cache
  • ultimate dev machine
  • unit testing
  • wcat
  • web guy
  • web service
  • webforms
  • xmlrpc
  • xunit
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.