class SknUtils::Configuration

Public Class Methods

new(params={}) click to toggle source

“filename-only” {config_filename: “filename-only”}

Calls superclass method
# File lib/skn_utils/configuration.rb, line 44
def initialize(params={})
  super()
  @_last_filelist = []
  @_base_path = './config/'
  @_default_mode = ENV['RACK_ENV'] || 'development'
  cfg_file = params.is_a?(String) ? params : (params[:config_filename] || @_default_mode)
  test_mode = ENV.fetch('TEST_GEM', 'rails').eql?('gem')
  config_path!( test_mode ? './spec/factories/' : './config/' )
  load_config_basename!(cfg_file)
end

Public Instance Methods

add_source!(file_path_or_hash) click to toggle source

:reload! required

# File lib/skn_utils/configuration.rb, line 84
def add_source!(file_path_or_hash) # load last
  return {} unless valid_file_path?(file_path_or_hash)
  @_last_filelist.push(file_path_or_hash).flatten
  self
end
config_path!(config_root) click to toggle source
# File lib/skn_utils/configuration.rb, line 55
def config_path!(config_root)
  @_base_path = config_root[-1].eql?('/') ? config_root : "#{config_root}/"
  self
end
load_and_set_settings(ordered_list_of_files) click to toggle source
# File lib/skn_utils/configuration.rb, line 65
def load_and_set_settings(ordered_list_of_files)
  reset_from_empty!( load_ordered_list( ordered_list_of_files ), false )
  self
end
Also aliased as: reload_from_files
load_config_basename!(environment_name=@_default_mode) click to toggle source
# File lib/skn_utils/configuration.rb, line 60
def load_config_basename!(environment_name=@_default_mode)
  reset_from_empty!(load_config(environment_name), false) # enable dot notation via defined methods(true) vs method_missing(false)
  self
end
prepend_source!(file_path_or_hash) click to toggle source

:reload! required

# File lib/skn_utils/configuration.rb, line 91
def prepend_source!(file_path_or_hash) # load first
  return {} unless valid_file_path?(file_path_or_hash)
  @_last_filelist.unshift(file_path_or_hash).flatten
  self
end
reload!() click to toggle source
# File lib/skn_utils/configuration.rb, line 71
def reload!
  reset_from_empty!( load_ordered_list(@_last_filelist), false )
  self
end
reload_from_files(ordered_list_of_files)
setting_files(config_root, environment_name) click to toggle source

Config.setting_files(“/path/to/config_root”, “your_project_environment”)

# File lib/skn_utils/configuration.rb, line 77
def setting_files(config_root, environment_name) # returns a file array
  config_path!(config_root)
  @_default_mode = environment_name
  configuration_files(environment_name)
end

Private Instance Methods

configuration_files(environment_name=@_default_mode) click to toggle source
# File lib/skn_utils/configuration.rb, line 112
def configuration_files(environment_name=@_default_mode)
  [
      "#{@_base_path}settings.yml",
      "#{@_base_path}settings/#{environment_name}.yml",
      "#{@_base_path}environments/#{environment_name}.yml",
      "#{@_base_path}settings.local.yml",
      "#{@_base_path}settings/#{environment_name}.local.yml",
      "#{@_base_path}environments/#{environment_name}.local.yml"
  ]
end
load_config(conf) click to toggle source
# File lib/skn_utils/configuration.rb, line 147
def load_config(conf)
  load_ordered_list( configuration_files(conf) )
end
load_ordered_list(filelist) click to toggle source
# File lib/skn_utils/configuration.rb, line 123
def load_ordered_list(filelist)
  settings_file = nil
  yname = filelist.first

  # maintain last list for :reload
  @_last_filelist = filelist unless @_last_filelist === filelist

  settings_file = yname if !!yname && yname.include?('settings.yml')

  f_base  = {}
  filelist.each do |filepath|
    next unless valid_file_path?( filepath )
    settings_file = filepath if filepath.include?('settings.yml')
    f_env   = filepath.is_a?(Hash) ? filepath : load_yml_with_erb(filepath)
    f_base  = f_base.deep_merge!(f_env) unless (f_env.nil? || f_env.empty?)
  end

  unless settings_file.nil?
    config_path!( File.dirname(settings_file) ) # maintain root dir
  end

  f_base
end
load_yml_with_erb(yml_file) click to toggle source
# File lib/skn_utils/configuration.rb, line 151
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
valid_file_path?(file_path_or_hash) click to toggle source
# File lib/skn_utils/configuration.rb, line 99
def valid_file_path?(file_path_or_hash)
  if file_path_or_hash.kind_of?(Hash)
    true

  elsif file_path_or_hash.kind_of?(String)
    File.exist?( file_path_or_hash )

  else
    false

  end
end