yml配置文件对象化

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

猜你喜欢

转载自schooltop.iteye.com/blog/2405774