简体   繁体   中英

Ruby on Rails YAML configuration design

I have a lot of static preferences for one entity. Do I need to store them in database table? Or I could use YAML configuration file, with approximate structure:

name: "Lights"
descriptions: ""
items: 
  title: "Fog lighs"
  description: "Should be used in foggy environment only!"

  title: "Xenon"
  description: "6500K"

name "Safety"
description: "Safety systems"
items:
  title: "Airbag"
  description: ""

  title: "ABS"
  description: "bla bla bla"

with ruby/object model markup . Can this be done with a nesting structure like in my example above?

Can somebody tell, if it is a good practice to do so? I don't want to use database, because these settings would be pretty much static, with low probability of need to change anything.

If it is possible, I would also like to know how to implement translation (i18n) for the elements of the structure (names and titles). I don't know where to put it in this particular situation.

Or I should use an entirely different approach?

You can of course use YAML in your rails app the same way you would from any ruby app:

require 'yaml'
things = YAML::load(open("my_things.yml"))

For translation just use the existing Rails I18n framework.

For example, given the classes:

class Thing
  attr_accessor :title, :description
end

class Lights < Thing
end

class Safety < Thing
end

And the following YAML data file:

---
- !ruby/object:Lights
  title: "Fog lights"
  description: "Should be used in foggy environments only!"
- !ruby/object:Lights
  title: "Xenon"
  description: "6500K"
- !ruby/object:Safety
  title: "Airbag"
- !ruby/object:Safety
  title: "ABS"
  description: "bla bla bla"

You can add the translation in config/locale/things.nb.yml:

nb:
  lights:
    fog_lights:
      title: "Tåkelys"
      description: "Kun til bruk i tåke!"
    xenon:
      title: "Xenon"
      description: "6500K"
  safety:
    airbag:
      title: "Kollisjonspute"
    abs:
      title: "ABS"
      description: "bla bla bla"

Similar for other languages, of course. Now let's implement a method to describe a thing:

class Thing
  def describe
    key = title.parameterize
    thing = self.class.to_s.downcase
    translated_title = I18n::t("#{thing}.#{key}.title")
    if description
      translated_description = I18n::t("#{thing}.#{key}.description")
    else
      translated_description = ""
    end
    "#{translated_title}: #{translated_description}"
  end
end

Assuming the current language is Norwegian, you can now do:

things.each do |thing|
  puts thing.describe
end

and get:

Tåkelys: Kun til bruk i tåke!
Xenon: 6500K
Kollisjonspute: 
ABS: bla bla bla

Notice you may either drop the description tags from the original YAML file, or have them as default fallbacks if there is no translation available.

Hope this helps.

you should have:

en:
  thing1:
    name: "Lights"
    descriptions: ""
   items:
      item1: 
        title: "Fog lighs"
        description: "Should be used in foggy environment only!"
      item2:
        title: "Xenon"
        description: "6500K"
es:
  thing1:
    name "translation"
    description: "translation"
    items:
      item1:
        title: "translation"
        description: "translation"
      item2:
        title: "translation"
        description: "translation"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM