Storage of constants


#1

Hello,

For a number of my models I have many constant key value pairs
(sometimes dozens). I have considered putting them in the model but it
ends up creating a very cluttered model file. Is there an approvaed
approach to having an external data file that is associated with a
model? Should I use a yaml file of the same name? xml? csv?
Recommendations are appreciated.

Thanks,
Ryan G.
http://www.prioninteractive.com


#2

Ryan G. wrote:

Hello,

For a number of my models I have many constant key value pairs
(sometimes dozens). I have considered putting them in the model but it
ends up creating a very cluttered model file. Is there an approvaed
approach to having an external data file that is associated with a
model? Should I use a yaml file of the same name? xml? csv?
Recommendations are appreciated.

Thanks,
Ryan G.
http://www.prioninteractive.com

You could have a subfolder of app/models called constants.

#app/models/foo.rb
require ‘constants/foo’
class Foo < AR::B
include FooConstants
self.bar = BAZ
end

#app/models/constants/foo.rb
module FooConstants
BAZ = “Foobar!”
end


#3

Hi –

On Sun, 14 Jan 2007, Ryan G. wrote:

Hello,

For a number of my models I have many constant key value pairs
(sometimes dozens). I have considered putting them in the model but it
ends up creating a very cluttered model file. Is there an approvaed
approach to having an external data file that is associated with a
model? Should I use a yaml file of the same name? xml? csv?
Recommendations are appreciated.

How about Ruby? :slight_smile: Just define some constants and pull them in at
runtime. You can put things in lib/ and then require them from your
model file(s), or from the bottom of environment.rb. For example, you
could create a file called extra_data.rb:

module ExtraData
COLORS = %w{ red orange yellow }
end

Then in your model file:

require ‘extra_data’

now you have access to ExtraData::COLORS.

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)