Sha256: df2bdd2b7debb4d2ced039136d4e731eb409483426fd73b2575366b9ce249e0e

Contents?: true

Size: 943 Bytes

Versions: 4

Compression:

Stored size: 943 Bytes

Contents

#Alow usage of other YAML config files.
#Each keys can be overwrited with an ENV variable prefixed by @env_prefix

#Example
#
### YAML FILE
# default: &default
#   some_key: "somedata"
# development:
#   <<: *default
# test:
#   <<: *default
# production:
#   <<: *default
#
### Application.rb
#
# CUSTOM_CONFIG=ExtraConfig.new(File.expand_path("config/my_file.yml",Rails.root),"SOMEPREFIX")
#
### Usage
#
# CUSTOM_CONFIG[:some_key]
# =>  "somedata"
#
# If ENV["SOMEPREFIX_SOME_KEY"]="other data"
#
# CUSTOM_CONFIG[:some_key]
# =>  "other data"

class ExtraConfig
  def initialize(file_path=nil, env_prefix="" , env=Rails.env)
    @env_prefix=env_prefix
    @yaml_config = (file_path && File.exist?(file_path)) ? YAML::load(File.open(file_path))[env] : {}
  end

  def [](k)
    key=k.to_s
    env_value(key)|| @yaml_config[key]
  end

  def env_value(key)
    env_var_name="#{@env_prefix}_#{key.to_s.upcase}"
    ENV[env_var_name]
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gorg_engine-1.2.4 config/extra_config.rb
gorg_engine-1.2.3 config/extra_config.rb
gorg_engine-1.2.0 config/extra_config.rb
gorg_engine-1.1.0 config/extra_config.rb