class DhEasy::Config::Local

Manage configuration from a file.

Attributes

local[R]

Configuration loaded from local configuarion file (see load).

@return [Hash,nil] `nil` when nothing has been loaded.

Public Class Methods

clear_cache() click to toggle source

Clear cache.

# File lib/dh_easy/config/local.rb, line 11
def self.clear_cache
  @@local = {}
end
default_file_path_list() click to toggle source

Default configuration file path list to be prioritized from first to last.

@return [Array<String>] Configuration file path list. Default is

`['./dh_easy.yaml', './dh_easy.yml']`
# File lib/dh_easy/config/local.rb, line 42
def self.default_file_path_list
  @@default_file_path_list ||= [
    './dh_easy.yaml',
    './dh_easy.yml'
  ]
end
load_file(file_path, opts = {}) click to toggle source

Load into or from cache a configuration file contents.

@param [String] file_path Configuration file path. @param [Hash] opts ({}) Configuration options. @option opts [Boolean] :force (false) Will reload configuration file

when `true`.

@return [Hash] Configuration file contents.

# File lib/dh_easy/config/local.rb, line 23
def self.load_file file_path, opts = {}
  opts = {
    force: false
  }.merge opts

  return {} if file_path.nil?

  @@local ||= {}
  key = file_path = File.expand_path file_path
  return @@local[key] if !opts[:force] && @@local.has_key?(key)

  @@local[key] = (YAML.load_file(file_path) rescue {}) || {}
  @@local[key].freeze
end
new(opts = {}) click to toggle source

Initialize.

@param [Hash] opts ({}) Configuration options (see load).

# File lib/dh_easy/config/local.rb, line 122
def initialize opts = {}
  @file_path_list = (opts.delete(:file_path_list) + []) unless opts[:file_path_list].nil?
  load opts
end

Public Instance Methods

[](key) click to toggle source

Get configuration key contents.

@param [String] key Configuration option key.

@return [Object,nil]

# File lib/dh_easy/config/local.rb, line 61
def [](key)
  local[key]
end
file_path() click to toggle source

Local configuration file path. It will lookup for the first valid file

at #file_path_list as default value.

@return [String] Configuration local file path.

# File lib/dh_easy/config/local.rb, line 80
def file_path
  @file_path ||= lookup_file_path
end
file_path_list() click to toggle source

Local configuration file path list. It will prioritize from first to last.

@return [Array<String>] Configuration local file path.

# File lib/dh_easy/config/local.rb, line 87
def file_path_list
  @file_path_list ||= self.class.default_file_path_list
end
load(opts = {}) click to toggle source

Loads a local configuration file.

@param [Hash] opts ({}) Configuration options. @option opts [String] :file_path (nil) Configuration file path to load (see

#file_path for configuration default file.)

@option opts [Boolean] :force (false) Will reload configuration file

when `true`.
# File lib/dh_easy/config/local.rb, line 98
def load opts = {}
  opts = {
    file_path: nil,
    force: false
  }.merge opts
  @file_path = opts[:file_path] || file_path
  @local = self.class.load_file(file_path, opts)
end
lookup_file_path() click to toggle source

Lookup file_path_list for the first valid file.

@return [String,nil] Valid file path or `nil`.

# File lib/dh_easy/config/local.rb, line 68
def lookup_file_path
  file_path_list.each do |candidate_file_path|
    next unless File.file?(File.expand_path(candidate_file_path))
    return candidate_file_path
  end
  nil
end
reload!() click to toggle source

Reloads local configuration file.

# File lib/dh_easy/config/local.rb, line 108
def reload!
  load force: true
end
reset!() click to toggle source

Reset instance to lookup for valid files from file_path_list and load

the first valid configuration file found.
# File lib/dh_easy/config/local.rb, line 114
def reset!
  @file_path = nil
  load force: true
end
to_h() click to toggle source

Convert to hash.

@return [Hash]

# File lib/dh_easy/config/local.rb, line 52
def to_h
  local
end