Last active
November 24, 2019 05:41
-
-
Save VishalTaj/d4378c039bc94d570aebcd2501f65d63 to your computer and use it in GitHub Desktop.
Adding settings options to Rails application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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