The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
class_attr_lookup

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Matt Williams

Posts: 24
Nickname: ottercat
Registered: May, 2007

Matt Williams is a subversive secretly replacing their java with ruby. Let's see if they notice.
class_attr_lookup Posted: May 23, 2007 2:12 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Matt Williams.
Original Post: class_attr_lookup
Feed Title: Subverting the Paradigm
Feed URL: http://subvertingtheparadigm.ottercat.net/feed/rss.xml
Feed Description: Which Paradigm? Pretty much all of them. It's about programming, exploring, management theory, development, and other radical notions. One day I'll talk about Ruby. The next day Rails. Javascript will be thrown in for good measure. Maybe even (gasp!) some Java. Thoughts about management style, too. A little bit of this, a little bit of that, all thrown together in a subversive attempt to change the world, but in a good way.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Matt Williams
Latest Posts From Subverting the Paradigm

Advertisement

I had a situation where I had configuration and/or control values in a database and I didn’t want to have to read them over and over again, so I’ve created the following.

For example, if I had a states table with the following values:
RUNNING
STARTING
STARTED
STOPPED
UNKNOWN
STOPPING

These values are used througout the database and the code as lookup values and my development database is not the same type of database as what is used in production, so rather than having to look them up each time because I can’t count on the id being the same in all cases, I’m using State.running to represent the RUNNING record in the states table.

Here’s the code:
1
2
3
4
5
6
7
8
9
10
11
12

  class Module
    def class_attr_lookup
      self.find(:all).each do |rec|
        r = rec.name.downcase.underscore
        self.class.send(:define_method, r) do
          self.class_eval "@@#{r}"
        end
        self.class_eval "@@#{r} = rec"
      end
    end
  end

I know that I could have used class_variable_get and class_variable_set, but I’m using both JRuby and MRI and at the time this was written there was a bug in JRuby and class_variable_get wasn’t implemented.

To use it, add class_attr_lookup to the model, like this:
1
2
3
4
5

  class State < ActiveRecord::Base
    class_attr_lookup
    validates_presence_of :name
  end

Then to access the RUNNING record, you’d use State.running.

Enjoy!

Read: class_attr_lookup

Topic: Dr Nic &raquo; Functional Testing using a Matrix to cover all edge cases [video] Previous Topic   Next Topic Topic: A Little Crazy Even For Me

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use