Skip to content

Instantly share code, notes, and snippets.

@VishalTaj
Last active November 24, 2019 05:41
Show Gist options
  • Save VishalTaj/d4378c039bc94d570aebcd2501f65d63 to your computer and use it in GitHub Desktop.
Save VishalTaj/d4378c039bc94d570aebcd2501f65d63 to your computer and use it in GitHub Desktop.
Adding settings options to Rails application.
require 'ostruct'
module RailsConfig
class Options < OpenStruct
PathNotFound = Class.new(StandardError)
attr_accessor :settings
def self.load_path
settings_path = Rails.root.join('config', 'settings.yml')
raise(PathNotFound, "'settings.yml' file is missing please add it inside 'config/'") unless settings_path.exist?
env_path = Rails.root.join('config', 'settings', "#{Rails.env}.yml")
raise(PathNotFound, "'#{Rails.env}.yml' file is missing please add it inside 'config/settings/'") unless env_path.exist?
settings_hash = YAML.load_file(settings_path) || {}
env_hash = YAML.load_file(env_path) || {}
return self.new(settings_hash.merge(env_hash))
end
end
end
Settings = RailsConfig::Options.load_path.freeze
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment