Re: Is there an Application state in Rails like ASP

Ezra Zygmuntowicz
Fri, 27 Oct 2006 00:31:25 -0000

Hi~

On Oct 26, 2006, at 4:42 PM, s.ross wrote:

>
> Unless I''m mistaken, anything you create in environment.rb
persists
> until server restart because environment.rb is a load-once file.
> There are several plugins for storing settings (n/v pairs), but if
> you want to persist page state like WinForms on Asp.Net, this
isn''t
> the method for you.
>
> If you''re casting about for a way to store options and
don''t want to
> go the plugin route, stick this in RAILS_ROOT/lib and they you can
> write code like:
>
> Options[:email_recipients] =
[''me-3Q2Tfjf0mexWk0Htik3J/w@public.gmane.org'',
''you-FBj3rGVNSac4Q++5jOxPmw@public.gmane.org'']
>
> # options.rb
> #
> # Provide a container for global option storage
> # Both getter/setter and [] access methods are
> # provided.
>
> class Options
>    @@option_hash = {}
>
>    def self.set(option_name, value)
>      option_name = option_name.to_s
>      @@option_hash[option_name] = value
>    end
>
>    def self.get(option_name)
>      option_name = option_name.to_s
>      @@option_hash[option_name]
>    end
>
>    def self.[]=(option_name, value)
>      option_name = option_name.to_s
>      self.set(option_name, value)
>    end
>
>    def self.[](option_name)
>      option_name = option_name.to_s
>      self.get(option_name)
>    end
>
>    def self.has_key?(key)
>      key = key.to_s
>      @@option_hash.has_key?(key)
>    end
>
>    def self.key?(key)
>      self.has_key?(key)
>    end
> end


	Keep in mind this will not work when you run more then one rails
backend. As each mongrel or fcgi will have a separate copy of this
class and you are not always garaunteed that your users will always
hit the same backend. So you will have to use the session, memcached
or a DRb server for global state because it needs to be an external
process that all rails backends can talk to to keep the data
consistent. Or you also use the database to store state too.


Cheers-

-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- ez-NLltGlunAUd/unjJdyJNww@public.gmane.org
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)