欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

yml配置文件对象化  

程序员文章站 2022-07-14 18:52:10
...
SETTING = Rails.application.config_for('settings/setting')

class Setting < OpenStruct

  def initialize(hash = nil)
    @table = {}
    @hash_table = {}

    if hash
      hash.each do |k, v|
        @table[k.to_sym] = v.is_a?(Hash) ? self.class.new(v) : v
        @hash_table[k.to_sym] = v

        new_ostruct_member(k)
      end
    end
  end

  def to_hash
    @hash_table
  end

end

Settings = Setting.new SETTING