github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

vigetlabs / constant_cache

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 26
    • 3
  • Source
  • Commits
  • Network (3)
  • Issues (0)
  • Downloads (2)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (2)
    • REL_0_0_2
    • REL_0_0_1
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Allows the caching of lookup data in your Rails models — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Fix release date 
reagent (author)
Mon Apr 14 22:53:32 -0700 2008
commit  ad131e4a8ce237cc95669476ab2916c2ab0b8891
tree    efdb104724cf004a017876700b4b6cc1ca3c12ff
parent  23558b93c214ef475a70a38b7e72ff909cebff1a
constant_cache /
name age
history
message
file .gitignore Mon Apr 14 22:41:42 -0700 2008 Improved documentation Add String#constant_name... [reagent]
file HISTORY Mon Apr 14 22:53:32 -0700 2008 Fix release date [reagent]
file MIT-LICENSE Mon Jun 25 21:11:51 -0700 2007 Start of constant_cache plugin (for Advanced Ra... [reagent]
file README Mon Apr 14 22:41:42 -0700 2008 Improved documentation Add String#constant_name... [reagent]
file Rakefile Mon Apr 14 22:43:12 -0700 2008 Minor change to description [reagent]
directory lib/ Mon Apr 14 22:41:42 -0700 2008 Improved documentation Add String#constant_name... [reagent]
directory spec/ Mon Apr 14 22:41:42 -0700 2008 Improved documentation Add String#constant_name... [reagent]
README
= Constant Cache

When your database has tables that store lookup data, there is a tendency 
to provide those values as constants in the model.  If you have an
account_statuses table with a corresponding model, your constants may look
like this:

  class AccountStatus
    ACTIVE   = 1
    PENDING  = 2
    DISABLED = 3
  end

There are a couple of problems with this approach:

As you add more lookup data to the table, you need to ensure that you're 
updating your models along with the data.  

The constants are stored as integer values and need to match up exactly 
with the data that's in the table (not necessarily a bad thing), but this
solution forces you to write code like this:

  Account.new(:username => 'preagan', :status => AccountStatus.find(AccountStatus::PENDING))

This requires multiple calls to find and obfuscates the code a bit.  Since classes
in Ruby are executable code, we can cache the objects from the database at runtime
and use them in your application.

== Installation

This code is packaged as a gem, so simply use the `gem` command to install:

  gem install constant_cache

== Example

"Out of the box," the constant_cache gem assumes that you want to use the 'name' column to generate
constants from a column called 'name' in your database table.  Assuming this schema:

  create_table :account_statuses do |t|
    t.string :name, :description
  end

  AccountStatus.create!(:name => 'Active',   :description => 'Active user account')
  AccountStatus.create!(:name => 'Pending',  :description => 'Pending user account')
  AccountStatus.create!(:name => 'Disabled', :description => 'Disabled user account')

We can use the plugin to cache the data in the table:

  class AccountStatus
    caches_constants
  end

Now you can write code that's a little cleaner and not use multiple unnecessary find calls:

  Account.new(:username => 'preagan', :status => AccountStatus::PENDING)

If the column you want to use as the constant isn't 'name', you can set that in the model. If
we have :name, :slug, and :description, we can use 'slug' instead:

  class AccountStatus
    caches_constants :key => :slug
  end
  
The value for the constant is truncated at 64 characters by default, but you can adjust this as
well:

  class AccountStatus
    caches_constants :limit => 16
  end

== Acknowlegements

Thanks to Dave Thomas for inspiring me to write this during his Metaprogramming talk at a Rails Edge 
conference in early 2007.

Copyright (c) 2007 Patrick Reagan of Viget Labs (mailto:patrick.reagan@viget.com), released under the MIT license
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server