Created
July 3, 2013 21:15
-
-
Save jphenow/5922894 to your computer and use it in GitHub Desktop.
Rails config file Object I like to use
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
class ConfigFile | |
attr_reader :config_name | |
attr_reader :context | |
def self.read_with_env(*args) | |
new(*args).read_with_env | |
end | |
def self.read(*args) | |
new(*args).read | |
end | |
def initialize(config_name, options = {}) | |
@config_name = config_name.to_s | |
@context = (options[:context] || "config").to_s | |
@bind = options[:binding] | |
end | |
def read | |
yamled | |
end | |
def read_with_env | |
read[Rails.env] | |
end | |
private | |
def yamled | |
YAML.load(erbed) | |
end | |
def erbed | |
ERB.new(file).result(@bind || binding) | |
end | |
def file | |
@file ||= File.read(Rails.root.join(context, "#{config_name}.yml")) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment