HighDots Forums  

Where to keep static arrays of data

Ruby On Rails Talk Ruby On Rails programming language mailing list


Discuss Where to keep static arrays of data in the Ruby On Rails Talk forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Zovar
 
Posts: n/a

Default Where to keep static arrays of data - 11-05-2009 , 08:55 PM






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

Reply With Quote
  #2  
Old   
bill walton
 
Posts: n/a

Default Re: Where to keep static arrays of data - 11-05-2009 , 09:06 PM






Hi,

On Thu, 2009-11-05 at 17:55 -0800, Zovar wrote:
Quote:
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #3  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: Where to keep static arrays of data - 11-05-2009 , 09:11 PM



bill walton wrote:
Quote:
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...

Quote:
HTH,
Bill
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org
--
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #4  
Old   
Craig White
 
Posts: n/a

Default Re: Where to keep static arrays of data - 11-05-2009 , 09:21 PM



On Fri, 2009-11-06 at 03:11 +0100, Marnen Laibow-Koser wrote:
Quote:
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #5  
Old   
Zovar
 
Posts: n/a

Default Re: Where to keep static arrays of data - 11-05-2009 , 09:26 PM



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

Reply With Quote
  #6  
Old   
Sijo k g
 
Posts: n/a

Default Re: Where to keep static arrays of data - 11-05-2009 , 11:42 PM



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

Reply With Quote
  #7  
Old   
Matt Jones
 
Posts: n/a

Default Re: Where to keep static arrays of data - 11-06-2009 , 05:22 PM



On Nov 5, 8:55*pm, Zovar <peter.zavor... (AT) gmail (DOT) com> wrote:
Quote:
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #8  
Old   
Zovar
 
Posts: n/a

Default Re: Where to keep static arrays of data - 11-06-2009 , 06:56 PM



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

Reply With Quote
  #9  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: Where to keep static arrays of data - 11-07-2009 , 12:40 AM



Matt Jones wrote:
Quote:
On Nov 5, 8:55�pm, Zovar <peter.zavor... (AT) gmail (DOT) com> 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 (AT) marnen (DOT) org
--
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 (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #10  
Old   
Foon
 
Posts: n/a

Default Re: Where to keep static arrays of data - 11-10-2009 , 05:03 PM



Hi,

Just a question regarding the following code

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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.