Sha256: 7064fe6bcb6aaa05476c860e495be05314e012fac4452cdd45c91bb39eaa78ca

Contents?: true

Size: 1.54 KB

Versions: 20

Compression:

Stored size: 1.54 KB

Contents

#
# @see SknSettings

module SknUtils

  class SknConfiguration < NestedResult

    def initialize(params={})
      default_mode = defined?(Rails) ? Rails.env : ENV.fetch('RAILS_ENV', 'development') 
      @config_filename = params.is_a?(String) ? params : params.fetch(:config_filename, default_mode)
      @base_path = ENV.fetch('TEST_GEM', 'rails').eql?('gem') ? './spec/factories/' : './config/'
      load_config_basename!(@config_filename)
    end

    def load_config_basename!(conf)
      reset_from_empty!(load_config(conf), false) # enable dot notation via defined methods vs method_missing
      self
    end

    def config_path!(fpath)
      @base_path = fpath if File.exist?("#{fpath}settings.yml")
    end

    private

    def load_config(conf)
      yname   = "#{@base_path}settings.yml"
        return {} unless File.exist?(yname)
        f_base  = load_yml_with_erb(yname)

      yname   = "#{@base_path}settings/#{conf}.yml"
        f_env   = load_yml_with_erb(yname) if File.exist?(yname)
        f_base  = f_base.deep_merge!(f_env) unless (f_env.nil? || f_env.empty?)

      yname   = "#{@base_path}settings/#{conf}.local.yml"
        f_local = load_yml_with_erb(yname)  if File.exist?(yname)
        f_base  = f_base.deep_merge!(f_local) unless (f_local.nil? || f_local.empty?)

      f_base
    end

    def load_yml_with_erb(yml_file)
      erb = ERB.new(File.read(yml_file)).result
      erb.empty? ? {} : Psych.load(erb)
    rescue => e
      puts "#{self.class.name}##{__method__} Class: #{e.class}, Message: #{e.message}"
      {}
    end
  end

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
skn_utils-3.6.0 lib/skn_utils/skn_configuration.rb
skn_utils-3.5.2 lib/skn_utils/skn_configuration.rb
skn_utils-3.5.0 lib/skn_utils/skn_configuration.rb
skn_utils-3.4.2 lib/skn_utils/skn_configuration.rb
skn_utils-3.4.1 lib/skn_utils/skn_configuration.rb
skn_utils-3.4.0 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.12 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.11 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.10 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.9 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.8 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.7 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.6 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.5 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.4 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.3 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.2 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.1 lib/skn_utils/skn_configuration.rb
skn_utils-3.3.0 lib/skn_utils/skn_configuration.rb
skn_utils-3.2.1 lib/skn_utils/skn_configuration.rb