How to cache an xml file and read its attributes in Rails? - ruby-on-rails

I have a file that represents all components of a game. I want to load the file into cache on boot, then have the ability to call the attributes of the file from the controllers. How do I begin?

I like http://cobravsmongoose.rubyforge.org for most simple XML handling:
CobraVsMongoose.xml_to_hash(File.open('path/to/xml').gets)
As for your specific case, I would add an initializer which requires cvm and sets the value above to a constant, which you could then access wherever you want...
# config/initializers/load_xml.rb
require 'cobravsmongoose'
MY_XML = CobraVsMongoose.xml_to_hash(File.open('path/to/xml').gets)

Try out REXML, its an XML parsing library for Ruby. I think it comes with the standard version of Ruby, so you shouldn't even need to install a gem.

Related

i18n rails get translations from two different yml files

I am using tolk for translation, but tolk takes all my values from en.yml and dumpes them in es.yml overwriting the existing content .
There are some stuff i don't want to be overwritten , so when i am searching for the es translations, i want rails to look in both es.yml and es.defaults.yml
( and so, i can keep isolated what i generate with tolk, and what remais the same )
Is there a way i can do this?
Thanks
Rails loads every file in the config/locales/ directory, so it will probably already work like you're suggesting. You can even organize it further than that, according to the I18n Guide:
http://guides.rubyonrails.org/i18n.html#organization-of-locale-files
However, I think that with duplicate key structures, Rails will probably override the values of the earlier loaded (sorted by file name) locale file with the values of the later loaded file. So please try to avoid duplicate keys.

Symfony: multiple i18n sources

For my project, I need to store translations in the database, for which I've implemented doctrine data source. However, I would like to leave standard translations (sf_admin and messages) in xml and keep them under source control. Is it possible to have 2 i18n instances that use different data sources? Or maybe one instance that can load data from different sources according to dictionary name?
I don't think there is a solution that doesn't require overriding sfI18n. An sfMessageSource_Aggregate exists but it seems nearly impossible to configure factories.yml to initialize it correctly.
You probably need to implement your own sfI18n::createMessageSource, that constructs the Aggregate passing the different sources in the constructor.

Rails Generator: generate files based on already existing rails files

I wanted to make a generator that created files (and directories, etc...) based on already existing files in the app (for instance, the views or controllers). So if we had views set up like this
-app
-views
- layouts
- application.html.erb
- users
- index.html.erb
- show.html.erb
- etc ...
and I wanted to create files based on them I can do (with just ruby)
directories = Dir.entries("#{Rails.root}/app/views")
directories.each do |directory|
unless directory == "." or directory == ".."
files = Dir.entries("#{Rails.root}/app/views/#{directory}")
files.each do |file|
unless file == "." or file == ".."
text = File.read("#{Rails.root}/app/views/#{directory}/#{file}")
something #=> whatever else needs to go here to edit the file
something else #=> output_file.puts whatever
end
end
end
end
so this is basically what I would like to do with a generator so I can roll my code into a plugin and use it for other apps.
First question, how can I generate arbitrary files (with filenames based on existing filenames using the generator. Is it appropriate to cycle through the directories like I did above, grab the directory/file and generate files? Is there a way to do what I did using a simpler method (mine seems easily breakable).
Also, should I put all that read/format/write code inside the generator itself and just pass a string into the "initialize content" section of create_file or should I put it somewhere else. Or should I use the generator to create the bare files and populate it with an init script?
Is there a more rails type of way of populating generated files, or should I just shove all my formatting code inside the generator. If so, what is the appropriate way to approach this.
I am not sure if you want to know how generators are built in rails3 or not. The code you are showing is not very generator-like. In generators you can use all commands from Thor, which offers you a very powerful toolset of manipulating files, and injecting code (strings) into classes or files.
So I would most definitely fill your files inside a generator, because then it happens on user request, and the user can choose whether or not certain files need or can be overwritten or not.
Inside your gem, you will have a lib/generators folder, containing a templates folder, containing all files you might want to place inside the rails application.
From the Thor documentation, here is a nice example to construct files in a generator.
Hope this helps.
there's a simple API to use generators in Rails. here you can find a good guide:
http://guides.rubyonrails.org/generators.html
if you want to check some code:
https://github.com/coderloop/tamed_beast (I'm the author of its generators)
https://github.com/pilu/web-app-theme (another clean example)

Organising Rails source files

If i want to move /lib/foo_bar.rb to /lib/tidy/foo_bar.rb or even /lib/tidy/somestuff/foo_bar.rb
must i declare FooBar to be module Tidy or module Tidy::Somestuff
in other words must the modules match the directory structure?
Yes, if you don't want to specify the load path. You can add lib/tidy to the LOAD_PATH and then Rails will find it, but it's easier to just stick with the conventions

Need to load contents of XML file into a constant variable, when to do this?

I want to load a xml file into a collection, and I will need to access this on each and every page request.
I only need to load it once, where/what point should I do this?
You can create an initializer to load the xml file and put into a constant.
config/initializers/load_xml_collection.rb
The most straight-forward way would be to set it up as an initializer. Create a new file in #{Rails.root}/config/initializers called load_xml_file.rb (or something a little more descriptive)
Then, within that, you could do something along the lines of:
SETTINGS_FROM_XML_FILE = method_to_read_xml
This will be executed once when your app is loaded. You'll also be able to access SETTINGS_FROM_XML_FILE anywhere in your application.
The only caveat is that if the file changes, you'll need to re-start the application, or come up with a more sophisticated way of loading the details you need.

Resources