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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 6 Sep 2008, at 18:08, javinto wrote:> > 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. >Did you restart your app after you added this ? Fred> 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 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
On Sep 6, 12:08 pm, javinto
<news4...-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org>
wrote:> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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.
On Sep 6, 1:34 pm, Andrew Bloom
<akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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
>
> On Sep 6, 12:08 pm, javinto
<news4...-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org> wrote:
>
> > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 On 6 sep, 20:38, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > On Sep 6, 1:34 pm, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 > > > On Sep 6, 12:08 pm, javinto <news4...-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org> wrote: > > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Most of the Rails libraries use the following pattern: class Something @@config1 = "change_me" cattr_accessor :config1 end Something.config1 = "secret code" On Sep 7, 10:38 am, javinto <news4...-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org> wrote:> 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 > > On 6 sep, 20:38, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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. > > > On Sep 6, 1:34 pm, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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 > > > > On Sep 6, 12:08 pm, javinto <news4...-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org> wrote: > > > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
On 8 sep, 18:52, Andrew Bloom
<akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Most of the Rails libraries use the following pattern:
>
> class Something
> @@config1 = "change_me"
> cattr_accessor :config1
> end
>
> Something.config1 = "secret code"
>
> On Sep 7, 10:38 am, javinto
<news4...-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org> wrote:
>
> > 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
> >configurethings 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
>
> > On 6 sep, 20:38, Andrew Bloom
<akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > > 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.
>
> > > On Sep 6, 1:34 pm, Andrew Bloom
<akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > > > 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
>
> > > > On Sep 6, 12:08 pm, javinto
<news4...-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org> wrote:
>
> > > > > 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 wannaconfigure
> > > > > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---