Class: DhEasy::Config::Local

Inherits:
Object
  • Object
show all
Defined in:
lib/dh_easy/config/local.rb

Overview

Manage configuration from a file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Local

Initialize.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options (see #load).



122
123
124
125
# 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

Instance Attribute Details

#localHash? (readonly)

Configuration loaded from local configuarion file (see #load).

Returns:

  • (Hash, nil)

    `nil` when nothing has been loaded.



8
9
10
# File 'lib/dh_easy/config/local.rb', line 8

def local
  @local
end

Class Method Details

.clear_cacheObject

Clear cache.



11
12
13
# File 'lib/dh_easy/config/local.rb', line 11

def self.clear_cache
  @@local = {}
end

.default_file_path_listArray<String>

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

Returns:

  • (Array<String>)

    Configuration file path list. Default is `['./dh_easy.yaml', './dh_easy.yml']`



42
43
44
45
46
47
# 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 = {}) ⇒ Hash

Load into or from cache a configuration file contents.

Parameters:

  • file_path (String)

    Configuration file path.

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :force (Boolean) — default: false

    Will reload configuration file when `true`.

Returns:

  • (Hash)

    Configuration file contents.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

Instance Method Details

#[](key) ⇒ Object?

Get configuration key contents.

Parameters:

  • key (String)

    Configuration option key.

Returns:

  • (Object, nil)


61
62
63
# File 'lib/dh_easy/config/local.rb', line 61

def [](key)
  local[key]
end

#file_pathString

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

at #file_path_list as default value.

Returns:

  • (String)

    Configuration local file path.



80
81
82
# File 'lib/dh_easy/config/local.rb', line 80

def file_path
  @file_path ||= lookup_file_path
end

#file_path_listArray<String>

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

Returns:

  • (Array<String>)

    Configuration local file path.



87
88
89
# 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 = {}) ⇒ Object

Loads a local configuration file.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Configuration options.

Options Hash (opts):

  • :file_path (String) — default: nil

    Configuration file path to load (see #file_path for configuration default file.)

  • :force (Boolean) — default: false

    Will reload configuration file when `true`.



98
99
100
101
102
103
104
105
# 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_pathString?

Lookup #file_path_list for the first valid file.

Returns:

  • (String, nil)

    Valid file path or `nil`.



68
69
70
71
72
73
74
# 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!Object

Reloads local configuration file.



108
109
110
# File 'lib/dh_easy/config/local.rb', line 108

def reload!
  load force: true
end

#reset!Object

Reset instance to lookup for valid files from #file_path_list and load

the first valid configuration file found.


114
115
116
117
# File 'lib/dh_easy/config/local.rb', line 114

def reset!
  @file_path = nil
  load force: true
end

#to_hHash

Convert to hash.

Returns:

  • (Hash)


52
53
54
# File 'lib/dh_easy/config/local.rb', line 52

def to_h
  local
end