# Configuring custom library

**URL:** <https://www.ruby-forum.com/t/configuring-custom-library/147898>\
**Category:** Rails\
**Created:** [September 6, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/configuring-custom-library/147898 "2008-09-06T19:33:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [September 6, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/configuring-custom-library/147898/1 "2008-09-06T19:33:42Z")

</div>

Hi,

I’ve added a custom library called lib\AccountSystem like so:

"module AccountSystem  
SINGLE = 1  
MULTIPLE = 2

class \<\< self  
attr\_accessor :account\_system\_type  
end  
end"

Now I wanna configure  
AccountSystem.account\_system\_type=AccountSystem::SINGLE in one app. I  
used an initializer: config/initializers/account\_initialization.rb  
where I put this line in.

I included my AccountSystem in the ApplicationController.

So now I’d like to check within my controllers the value of  
AccountSystem.account\_system\_type

But there it is empty!  
However if I run “Ruby script\console” and type  
AccountSystem.account\_system\_type I get the value of 1 as I would  
expect.

How can I achieve the same result within my controllers?

I’m on rails 2.1.0/2.1.1

Thanks

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [September 6, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/configuring-custom-library/147898/2 "2008-09-06T19:34:52Z")

</div>

On 6 Sep 2008, at 18:08, javinto wrote:

> ```
> attr_accessor :account_system_type
> 
> ```
> 
> end  
> end"
> 
> Now I wanna configure  
> AccountSystem.account\_system\_type=AccountSystem::SINGLE in one app. I  
> used an initializer: config/initializers/account\_initialization.rb  
> where I put this line in.

Did you restart your app after you added this ?

Fred

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [September 6, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/configuring-custom-library/147898/3 "2008-09-06T20:35:10Z")

</div>

if you are checking AccountSystem.account\_system\_type in your  
controller what is the point of including it in ApplicationController?  
If you plan to use it as such, maybe you are better off making it a  
Singleton Class.

If you actually want account\_system\_type to be a class accessor on  
ApplicationController you must do something like this:

Module AccountSystem  
SINGLE = 1  
MULTIPLE = 2

self.included(klass)  
klass.send(:cattr\_accessor, :account\_system\_id)  
end  
end

class ApplicationController \< ActionController::Base  
include AccountSystem

# this is how you would use it

def random\_method  
self.class.account\_system\_id == AccountSystem::SINGLE  
end  
end

That should do what you seem to want, but if your goal was something  
different let me know and I can try to help.

Of note, if you do use the above solution you could do:

Module AccountSystem  
…

def single?  
self.class.account\_system\_id == SINGLE  
end

def multiple?  
self.class.account\_system\_id == MULTIPLE  
end

…  
end

class ApplicationController \< ActionController::Base  
include AccountSystem

# this is how you would use it

def random\_method  
if single?  
puts “single”  
elsif multiple?  
puts “multiple”  
else  
raise “please set the AccountSystem.acoun\_type\_id”  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [September 6, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/configuring-custom-library/147898/4 "2008-09-06T20:39:16Z")

</div>

What I forgot to mention is that if you do:

module AccountSystem  
class \<\< self  
attr\_accessor :account\_system\_type  
end  
end

You are actually putting the attr\_accessor on the class Module, not on  
the class that AccountSystem will be included in. You need to use the  
included method, or use the extend method and refactor your module a  
bit.

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [September 8, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/configuring-custom-library/147898/5 "2008-09-08T21:34:06Z")

</div>

Most of the Rails libraries use the following pattern:

class Something  
@@config1 = “change\_me”  
cattr\_accessor :config1  
end

Something.config1 = “secret code”

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [September 7, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/configuring-custom-library/147898/6 "2008-09-07T23:47:54Z")

</div>

Thanks!!

You made me dive into this matter deep this time. I get the picture  
now. That’s partially then. As I’m still looking for a way to  
configure things in a way, well like Rails does, e.g. in the  
initializers: ActiveSupport.escape\_html\_entities\_in\_json = false

But then, I don’t know wether ActiveSupport here is one big  
Singleton?! That might be a way to solve it as this functionality  
would perfectly fit in a Singleton. I will give it a thought…

Jan

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [September 8, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/configuring-custom-library/147898/7 "2008-09-08T22:51:20Z")

</div>

Andrew, thanks for the last ‘hint’!

It made me realise that I definitely tried to define a class in my  
module - see the original code - but did not declare class variables.  
So what I did now, was just change the following (see my original code  
above):  
class \<\< self  
mattr\_accessor :account\_system\_type  
end

Notice the MATTR\_accessor!

That’s it! It’s working now as if I hoped it would in the first place.  
By the way: cattr\_accessor works as well naturely.

I appreciate all the comments you made!

Jan
