# Changing class data in environmrnt.rb

**URL:** <https://www.ruby-forum.com/t/changing-class-data-in-environmrnt-rb/57691>\
**Category:** Rails\
**Created:** [April 13, 2006, 2:45am UTC](https://www.ruby-forum.com/t/changing-class-data-in-environmrnt-rb/57691 "2006-04-13T02:45:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [April 13, 2006, 2:45am UTC](https://www.ruby-forum.com/t/changing-class-data-in-environmrnt-rb/57691/1 "2006-04-13T02:45:34Z")

</div>

Greetings,

I have a class that should allow you define some default values. It  
seems to make the most sense to do this environment.rb since its the  
default app configuration. It currently looks like this:

Settings::DEFAULT\_VALUES.merge!({  
:setting\_one =\> ‘foo’,  
:settings\_two =\> ‘bar’  
})

The problem is in development mode, the values are only there for the  
first request after launching webrick. After the first request the  
entries have dissapeared form the class. In development mode the  
changes persist and are around for any number of requests. Specifically  
it seems to be the cache\_class setting. It seems the class is destroyed  
and recreated when not cached for each request, although environment.rb  
is only run the first time.

I modeled this solution off of various rails settings, like:  
ActiveSupport::CoreExtensions::Time::Conversions::DATE\_FORMATS.merge!({:date  
=\> ‘%b %d, %Y’})

But it would seem that the core rails stuff is cached no matter what  
environment settings. So what is the best way to include configuration  
of a custom class that works in development mode? Running a  
before\_filter in application.rb seems like a messy solution.

Any ideas?

-Alex

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [April 16, 2006, 9:13am UTC](https://www.ruby-forum.com/t/changing-class-data-in-environmrnt-rb/57691/2 "2006-04-16T09:13:14Z")

</div>

No ideas at all?

Alex W. wrote:

> I have a class that should allow you define some default values. My  
> stuff in environment.rb currently looks like this:
> 
> Settings::DEFAULT\_VALUES.merge!({:setting\_one =\> ‘foo’})
> 
> The problem is in development mode, the values are only there for the  
> first request. After the first request the  
> entries have dissapeared form the class. In development mode the  
> changes persist and are around for any number of requests. Specifically  
> it seems to be the cache\_class setting. It seems the class is destroyed  
> and recreated when not cached for each request, although environment.rb  
> is only run the first time.
> 
> Anyway without a before\_filter to do this?

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [April 17, 2006, 2:10am UTC](https://www.ruby-forum.com/t/changing-class-data-in-environmrnt-rb/57691/3 "2006-04-17T02:10:02Z")

</div>

Alex W. wrote:

> > entries have dissapeared form the class. In development mode the  
> > changes persist and are around for any number of requests. Specifically  
> > it seems to be the cache\_class setting. It seems the class is destroyed  
> > and recreated when not cached for each request, although environment.rb  
> > is only run the first time.
> > 
> > Anyway without a before\_filter to do this?

Could you ask your question again?

I have something like this working in both production and development,  
but your question seems to have a typo involving the word development.

–

Ray

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [April 18, 2006, 12:58am UTC](https://www.ruby-forum.com/t/changing-class-data-in-environmrnt-rb/57691/4 "2006-04-18T00:58:57Z")

</div>

Alex W. wrote:

> So Ray, how exactly did you make this work in development mode?

I think it’s the merge! that is causing this problem. Where is Settings  
defined? Wherever that is is being reinitialized on every execution, but  
as you say, your environment.rb is only read the first time.

I just defined the whole config in environment.rb.

Here’s a snippet from my environment.rb

module EventsConfig  
PARAMS = {  
# Custom parameters for display of events

```
 # Default order for sorting events
 :default_sorting_key => "date",

 # Default number of events per page
 :default_per_page => 30,

 # When do cookies expire
 :days_until_login_cookies_expire => 14.days

```

}  
end

Then I access what I need by EventsConfig::PARAMS[:default\_per\_page]

Hope that helps.

–

Ray

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [April 17, 2006, 7:33pm UTC](https://www.ruby-forum.com/t/changing-class-data-in-environmrnt-rb/57691/5 "2006-04-17T19:33:59Z")

</div>

Ray B. wrote:

> Alex W. wrote:
> 
> > > entries have dissapeared form the class. In development mode the  
> > > changes persist and are around for any number of requests. Specifically  
> > > it seems to be the cache\_class setting. It seems the class is destroyed  
> > > and recreated when not cached for each request, although environment.rb  
> > > is only run the first time.
> > > 
> > > Anyway without a before\_filter to do this?
> 
> Could you ask your question again?
> 
> I have something like this working in both production and development,  
> but your question seems to have a typo involving the word development.
> 
> –
> 
> Ray

Indeed there was a confusing typo.

I have a class that should allow you define some default values. It  
seems to make the most sense to do this environment.rb since its the  
default app configuration. It currently looks like this:

Settings::DEFAULT\_VALUES.merge!({  
:setting\_one =\> ‘foo’,  
:settings\_two =\> ‘bar’  
})

The problem is in development mode, the values are only there for the  
first request after launching webrick. After the first request the  
entries have dissapeared form the class. In production mode the  
changes persist and are around for any number of requests. Specifically  
it seems to be the cache\_class setting. It seems the class is destroyed  
and recreated when not cached for each request, although environment.rb  
is only run the first time.

I modeled this solution off of various rails settings, like:  
ActiveSupport::CoreExtensions::Time::Conversions::DATE\_FORMATS.merge!({:date  
=\> ‘%b %d, %Y’})

But it would seem that the core rails stuff is cached no matter what  
environment settings. So what is the best way to include configuration  
of a custom class that works in development mode? Running a  
before\_filter in application.rb seems like a messy solution.

So Ray, how exactly did you make this work in development mode?

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [April 19, 2006, 12:56am UTC](https://www.ruby-forum.com/t/changing-class-data-in-environmrnt-rb/57691/6 "2006-04-19T00:56:27Z")

</div>

Ray B. wrote:

> Alex W. wrote:
> 
> > So Ray, how exactly did you make this work in development mode?
> 
> I think it’s the merge! that is causing this problem. Where is Settings  
> defined? Wherever that is is being reinitialized on every execution, but  
> as you say, your environment.rb is only read the first time.
> 
> I just defined the whole config in environment.rb.
> 
> Here’s a snippet from my environment.rb
> 
> module EventsConfig  
> PARAMS = {  
> # Custom parameters for display of events
> 
> ```
> # Default order for sorting events
> :default_sorting_key => "date",
> 
> # Default number of events per page
> :default_per_page => 30,
> 
> # When do cookies expire
> :days_until_login_cookies_expire => 14.days
> 
> ```
> 
> }  
> end
> 
> Then I access what I need by EventsConfig::PARAMS[:default\_per\_page]
> 
> Hope that helps.
> 
> –
> 
> Ray

Thank you. Using a module to house the settings is a great idea because  
it persists. And to make it as flexible as possible, I added this to my  
Settings class:

@@defaults = (defined?(SettingsDefaults) ? SettingsDefaults::DEFAULTS :  
{}).with\_indifferent\_access

making the SettingsDefaults module entirely optional.

Thanks for the tip!
