Where to store some static data needed for the app

Hi,

I want to store 6 palettes with color information.

This info should be accessed from a couple of places in my app. From one
controller and from one module I have.

I’m not sure what would be the best way to do it.

In some hashes?

red_palette = { :background => “abcdef”, :header => “fedefg” }
blue_paletter = { :background => “12ed34”, :header => “abdefg” }

In yml?

red_palette:
background: abcdef
header: fedefg

I guess I could make a small module and require it, but then I need to
require it two times. I’m starting to think about memory even though for
this it would be very cheap anyway.

Any advices? I’m on Rails 3.0.5
Thanks.

comopasta Gr wrote in post #1024192:

Hi,

I want to store 6 palettes with color information.

Would this make sense?

module Palettes
DARK_PALETTE = {
:background => “323232”,
:header => “4d4c4d” }
end

Then just:

require ‘palettes’
puts Palettes::DARK_PALETTE

I does the trick. I’d like to know it this is a good approach.

comopasta Gr wrote in post #1024192:

I guess I could make a small module and require it, but then I need to
require it two times. I’m starting to think about memory even though for
this it would be very cheap anyway.

Sounds like you’re confusing “include” with “require”. You could
“require” a file 50 times, but Ruby would loading it only once.

If you create a ruby module in would use “include” to include the module
into a class. But, it seems like a bit of overkill to create a module
that containing no methods, but only a single constant. I think I’d
simply put the constant on whatever class is dealing with your color
pallets.

Example:

class MyController
DARK_PALETTE = {
:background => “323232”,
:header => “4d4c4d”
}


end

Then from anywhere else you need the constant:

MyController::DARK_PALETTE

The other, probably better, option would be to setup a custom
configuration YAML file that includes all your application specific
constants that gets loaded at startup. That way application wide
settings are separated from the application’s code.

There was a Railscasts episode a good while back that showed an example
of setting up a YAML Configuration File:

On Sep 29, 2011, at 12:41 AM, comopasta Gr wrote:

Hi,

I want to store 6 palettes with color information.

This info should be accessed from a couple of places in my app. From one
controller and from one module I have.

I’m not sure what would be the best way to do it.

Settingslogic

-philip

A nice way to do this is to place a yml file in the config folder

like my_app.yml

and read it in application.rb
MY_CONFIG = YAML.load(File.read(“config/my_app.yml”)) unless defined?
(MY_CONFIG)

you can define different files for different environments like
my_app_test.yml
my_app_development.yml
my_app_production.yml

and load with
MY_CONFIG = YAML.load(File.read(“config/my_app_#{Rails.env}.yml”))
unless defined?(MY_CONFIG)