Class: DhEasy::Config::Local
- Inherits:
-
Object
- Object
- DhEasy::Config::Local
- Defined in:
- lib/dh_easy/config/local.rb
Overview
Manage configuration from a file.
Instance Attribute Summary collapse
-
#local ⇒ Hash?
readonly
Configuration loaded from local configuarion file (see #load).
Class Method Summary collapse
-
.clear_cache ⇒ Object
Clear cache.
-
.default_file_path_list ⇒ Array<String>
Default configuration file path list to be prioritized from first to last.
-
.load_file(file_path, opts = {}) ⇒ Hash
Load into or from cache a configuration file contents.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Get configuration key contents.
-
#file_path ⇒ String
Local configuration file path.
-
#file_path_list ⇒ Array<String>
Local configuration file path list.
-
#initialize(opts = {}) ⇒ Local
constructor
Initialize.
-
#load(opts = {}) ⇒ Object
Loads a local configuration file.
-
#lookup_file_path ⇒ String?
Lookup #file_path_list for the first valid file.
-
#reload! ⇒ Object
Reloads local configuration file.
-
#reset! ⇒ Object
Reset instance to lookup for valid files from #file_path_list and load the first valid configuration file found.
-
#to_h ⇒ Hash
Convert to hash.
Constructor Details
#initialize(opts = {}) ⇒ Local
Initialize.
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
#local ⇒ Hash? (readonly)
Configuration loaded from local configuarion file (see #load).
8 9 10 |
# File 'lib/dh_easy/config/local.rb', line 8 def local @local end |
Class Method Details
.clear_cache ⇒ Object
Clear cache.
11 12 13 |
# File 'lib/dh_easy/config/local.rb', line 11 def self.clear_cache @@local = {} end |
.default_file_path_list ⇒ Array<String>
Default configuration file path list to be prioritized from first to last.
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.
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. 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.
61 62 63 |
# File 'lib/dh_easy/config/local.rb', line 61 def [](key) local[key] end |
#file_path ⇒ String
Local configuration file path. It will lookup for the first valid file
at #file_path_list as default value.
80 81 82 |
# File 'lib/dh_easy/config/local.rb', line 80 def file_path @file_path ||= lookup_file_path end |
#file_path_list ⇒ Array<String>
Local configuration file path list. It will prioritize from first to last.
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.
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_path ⇒ String?
Lookup #file_path_list for the first valid file.
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.(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_h ⇒ Hash
Convert to hash.
52 53 54 |
# File 'lib/dh_easy/config/local.rb', line 52 def to_h local end |