简体   繁体   中英

Rails 4 - Yaml configuration file

I have this file config/application.yml

settings:
  info:
    name: MyAppName
    domain: example.com
  contact:
    email: mail@example.com
    phone: 1234567890

And in the environment.rb i have this

AppConfig = YAML::load_file('config/application.yml')

So now i can access this by using AppConfig["settings"]["info"]["name"]

How can i access this by using AccConfig.settings.info.name ?

WITHOUT ANY GEMS!

If you are using Rails 4.2 or higher, you can use config_for for the config files. They need to be placed under /config folder. (haven't tried otherwise)

In your case it would be: config = Rails.application.config_for(:application)

This is more clear and Rails way to load configs into application.

Then you can use OpenStruct to have dot notation enabled for it.

APP_CONFIG = OpenStruct.new(config)

Here's an easy way to do this without any gems, though I'm not sure about the performance if you are doing this frequently.

The idea is to first convert the data to JSON, and then parse the JSON to OpenStruct (which is built into Ruby):

json_data = YAML::load_file('config/application.yml').to_json
data = JSON.parse(json_data, object_class: OpenStruct)

This will deeply convert all hashes to OpenStructs and also correctly handles arrays.

As an example, if I have this YAML:

people:
  -
    name: 'Jerry Rasmussen'
    address:
      address_1: '123 Street St'
  -
    name: 'Sara DeWetzel'

Then it can be loaded and used like so:

json_data = YAML::load_file('config/people.yml').to_json
data = JSON.parse(json_data, object_class: OpenStruct)

data.people.first.name
 => Jerry Rasmussen
data.people.first.address.address_1
 => 123 Street St

There is no built-in way to convert a Hash into a construct that's accessible via dot syntax. You can either use a gem like settingslogic gem and point it to your application.yml file OR take a look at the source to find out the process by which this is done. I think the easiest and most robust approach is to use a popular (read: well tested in the wild) and well-documented gem vs. rolling your own.

# app/models/settings.rb
class Settings < Settingslogic
  source "#{Rails.root}/config/application.yml"
  namespace Rails.env
end

You can then access the individual settings via

Settings.info.name
# MyAppName

You can use recursive-open-struct gem

app_config = YAML.load_file('config/application.yml').with_indifferent_access
ros = RecursiveOpenStruct.new(app_config)

puts ros.settings.info.name # MyAppName

You can install hash dot gem

And, then use following code.

require 'hash_dot'
AppConfig = YAML::load_file('config/application.yml')

and, now call

AppConfig.settings.info.name

It will return your desired out put.

You could look into rails native

ActiveSupport::OrderedOptions < Hash

You still would have to convert your hashes with some function though, but as a starting point, if you really don't want any gem.

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