Configuring custom library

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

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

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

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.

Most of the Rails libraries use the following pattern:

class Something
@@config1 = “change_me”
cattr_accessor :config1
end

Something.config1 = “secret code”

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

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