Where to keep static arrays of data

View: New views
10 Messages — Rating Filter:   Alert me  

Where to keep static arrays of data

by Zovar :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hello,

I was wondering if there is a right way to keep some static arrays of
data inside the application. I want to be able to access them
globally. (it's not for setting vars, just simple arrays, i.e. ['ICQ',
'AIM', 'GTALK'] etc). Also I don't wan't to use a database for that,
need a hardcoded option.

Read o the forum, that some people use environment.rb, but wasn't sure
about that. I would like to have a separate file for each array of
data, but what is the best practise for that? Just create rb files in
a separate folder?
--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Where to keep static arrays of data

by bill walton-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

On Thu, 2009-11-05 at 17:55 -0800, Zovar wrote:
> Hello,
>
> I was wondering if there is a right way to keep some
> static arrays of data inside the application.

I'd consider storing them in the models to which they relate.  If they
need their own models, that's easy too.  The models don't have to
inherit from ActiveRecord.

HTH,
Bill


--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Where to keep static arrays of data

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


bill walton wrote:

> Hi,
>
> On Thu, 2009-11-05 at 17:55 -0800, Zovar wrote:
>> Hello,
>>
>> I was wondering if there is a right way to keep some
>> static arrays of data inside the application.
>
> I'd consider storing them in the models to which they relate.  If they
> need their own models, that's easy too.  The models don't have to
> inherit from ActiveRecord.

I'm about to try something kind of similar.  I'm going to be
experimenting with storing state and country info outside the DB, since
it's static and relatively small.  I hope to use one of the tableless AR
libraries to make the data act like an AR model, though.  Perhaps I'll
find out why no one does this... :)

>
> HTH,
> Bill

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@...
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Where to keep static arrays of data

by Craig White-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Fri, 2009-11-06 at 03:11 +0100, Marnen Laibow-Koser wrote:

> bill walton wrote:
> > Hi,
> >
> > On Thu, 2009-11-05 at 17:55 -0800, Zovar wrote:
> >> Hello,
> >>
> >> I was wondering if there is a right way to keep some
> >> static arrays of data inside the application.
> >
> > I'd consider storing them in the models to which they relate.  If they
> > need their own models, that's easy too.  The models don't have to
> > inherit from ActiveRecord.
>
> I'm about to try something kind of similar.  I'm going to be
> experimenting with storing state and country info outside the DB, since
> it's static and relatively small.  I hope to use one of the tableless AR
> libraries to make the data act like an AR model, though.  Perhaps I'll
> find out why no one does this... :)
----
I wouldn't say that no one does this... and note to the OP, I use
app/helpers/application_helper.rb so these constants are available
anywhere in the appliction

$ cat /app/helpers/application_helper.rb
# Methods added to this helper will be available to all templates in the
application.
module
ApplicationHelper                                                            

  # Select List of Yes / No
  YES_NO = [
    [ "Yes", true ],
    [ "No",  false ]
  ].freeze unless defined? YES_NO

  # acl
  SECURITY_LEVEL = {
     "payables_write_checks" => 14,
     "payables_accept_checks" => 14,
     "payables_print_checks" => 14,
     "payables_print_the_checks" => 14,
     "reports_inventory_reports_view" => 3,
     "reports_price_list_w_costs" => 5,
     "reports_price_list_w_costs_export" => 5,
     "reports_price_list_wo_costs" => 4,
     "reports_price_list_wo_costs_export" => 4,
     "reports_payable_reports_view" => 6,
     "reports_commissions_print" => 8,
     "reports_commissions_export" => 8,
     "reports_tax_reports_view" => 12,
     "reports_state_taxes_print" => 8,
     "reports_state_taxes_export" => 8,
     "reports_sco_taxes_print" => 8,
     "reports_mes_taxes_print" => 8,
     "reports_phx_taxes_print" => 8,
     "reports_peo_taxes_print" => 8,
     "reports_sco_taxes_export" => 8,
     "reports_mes_taxes_export" => 8,
     "reports_phx_taxes_export" => 8,
     "reports_peo_taxes_export" => 8,
     "reports_receivable_reports_view" => 8,
     "reports_customers_all_print" => 8,
     "reports_customers_all_export" => 8,
     "reports_customers_by_salesman_print" => 8,
     "reports_customers_by_salesman_export" => 8,
     "reports_customer_codes" => 8,
     "reports_branch_codes" => 8,
     "users_list" =>  14
  }.freeze unless defined? SECURITY_LEVEL

Craig


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Where to keep static arrays of data

by Zovar :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks for you replies.

I would be very grateful if you could give me an example of how to
store it in the model... Should I create local collection, or define a
method for it? Also how would I access this data from a controller?



On Nov 6, 2:06 am, bill walton <bwalton...@...> wrote:

> Hi,
>
> On Thu, 2009-11-05 at 17:55 -0800, Zovar wrote:
> > Hello,
>
> > I was wondering if there is a right way to keep some
> > static arrays of data inside the application.
>
> I'd consider storing them in the models to which they relate.  If they
> need their own models, that's easy too.  The models don't have to
> inherit from ActiveRecord.
>
> HTH,
> Bill
--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Where to keep static arrays of data

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi Zovar

     You can do it different ways.one method is create a file say
global_data.rb in the folder config/initializers  Then define these
array there.You can also define also a module there like

in global_data.rb

module GlobalData
   my_array = ['ICQ','AIM', 'GTALK']
end

     Now you can access this from anywhere in your application like
GlobalData::my_array

       If you want you can freeze this also like

module GlobalData
   my_array = ['ICQ','AIM', 'GTALK']
end
GlobalData.freeze

Or if you want global variables(Please avoid its use as far as possible)
you can do like inside this same file global_data.rb

$my_array = ['ICQ','AIM', 'GTALK']

      So from anywhere you can access it like $my_array

          You can also think of defining my_array above as constant if
needed




Sijo
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Where to keep static arrays of data

by Matt Jones :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message




On Nov 5, 8:55 pm, Zovar <peter.zavor...@...> wrote:

> Hello,
>
> I was wondering if there is a right way to keep some static arrays of
> data inside the application. I want to be able to access them
> globally. (it's not for setting vars, just simple arrays, i.e. ['ICQ',
> 'AIM', 'GTALK'] etc). Also I don't wan't to use a database for that,
> need a hardcoded option.
>
> Read o the forum, that some people use environment.rb, but wasn't sure
> about that. I would like to have a separate file for each array of
> data, but what is the best practise for that? Just create rb files in
> a separate folder?

Some good solutions here already, but one that I didn't see mentioned
was YAML. It's overkill for storing a small number of things, but can
be handy if you need a big hash table.

Basically, you have a regular YAML file (call it config/
foo_lookup.yml, for instance) and then do this in a model:

class SomeModel < AR::Base
  FOO_LOOKUP = YAML::load_file(File.join(RAILS_ROOT, 'config',
'foo_lookup.yml'))
end

Then you can use it pretty much anywhere as SomeModel::FOO_LOOKUP.
Note that since it runs at the class level, it will only get loaded
once in production.

The other (small) nice thing with this setup is that it keeps changes
to the constants apart from the model - handy if you're ever poring
over source control logs...

--Matt Jones

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Where to keep static arrays of data

by Zovar :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks a lot!
--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Where to keep static arrays of data

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Matt Jones wrote:
> On Nov 5, 8:55�pm, Zovar <peter.zavor...@...> wrote:
>> data, but what is the best practise for that? Just create rb files in
>> a separate folder?
>
> Some good solutions here already, but one that I didn't see mentioned
> was YAML.
[...]

Yup.  That's what I'll be using.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@...
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Where to keep static arrays of data

by Nicolas Gaiffe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

Just a question regarding the following code

> class SomeModel < AR::Base
>   FOO_LOOKUP = YAML::load_file(File.join(RAILS_ROOT, 'config',
> 'foo_lookup.yml'))
> end

Why should the model inherit from ActiveRecord::Base ?
Would the array be loaded only once if such a class was in the lib
(without AR inheritance) instead of app/models (which may require the
inheritance) ?

Thanks for your help
I'm quite interested in keeping static arrays like that in memory but
having YAML files constantly being read and structures filled would
ruin all targeted performance gain.
--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---