Class: Committer::Config::Accessor
- Inherits:
-
Object
- Object
- Committer::Config::Accessor
- Includes:
- Singleton
- Defined in:
- lib/committer/config/accessor.rb
Class Method Summary collapse
-
.reload ⇒ Object
Class method for reload.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Accessor for the loaded config.
-
#initialize ⇒ Accessor
constructor
A new instance of Accessor.
- #load_config ⇒ Object
- #load_default_file(file_name) ⇒ Object
- #load_yml_file(contents) ⇒ Object
- #read_file_from_git_root(file_name) ⇒ Object
- #read_file_from_home(file_name) ⇒ Object
- #read_file_from_path(path) ⇒ Object
- #read_path_prioritized_file(file_name) ⇒ Object
-
#reload ⇒ Object
Force reload configuration (useful for testing).
-
#to_h ⇒ Object
Get the entire config hash.
Constructor Details
#initialize ⇒ Accessor
Returns a new instance of Accessor.
15 16 17 |
# File 'lib/committer/config/accessor.rb', line 15 def initialize @config = load_config end |
Class Method Details
.reload ⇒ Object
Class method for reload
85 86 87 |
# File 'lib/committer/config/accessor.rb', line 85 def self.reload instance.reload end |
Instance Method Details
#[](key) ⇒ Object
Accessor for the loaded config
20 21 22 |
# File 'lib/committer/config/accessor.rb', line 20 def [](key) @config[key.to_sym] || @config[key.to_s] end |
#load_config ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/committer/config/accessor.rb', line 29 def load_config # Load configs from both locations and merge them home_config = load_yml_file(read_file_from_home(Committer::Config::Constants::CONFIG_FILE_NAME)) git_root_config = load_yml_file(read_file_from_git_root(Committer::Config::Constants::CONFIG_FILE_NAME)) raise Committer::ConfigErrors::NotSetup if home_config.empty? && git_root_config.empty? unless home_config.is_a?(Hash) && git_root_config.is_a?(Hash) raise Committer::ConfigErrors::FormatError, 'Config file must be a YAML hash' end # Merge configs with git root taking precedence home_config.merge(git_root_config) end |
#load_default_file(file_name) ⇒ Object
63 64 65 |
# File 'lib/committer/config/accessor.rb', line 63 def load_default_file(file_name) read_file_from_path(File.join(Committer::Config::Constants::DEFAULTS_PATH, file_name)) end |
#load_yml_file(contents) ⇒ Object
45 46 47 |
# File 'lib/committer/config/accessor.rb', line 45 def load_yml_file(contents) YAML.safe_load(contents, permitted_classes: [Symbol, NilClass, String, Array]) || {} end |
#read_file_from_git_root(file_name) ⇒ Object
55 56 57 |
# File 'lib/committer/config/accessor.rb', line 55 def read_file_from_git_root(file_name) read_file_from_path(File.join(Committer::GitHelper.repo_root, '.committer', file_name)) end |
#read_file_from_home(file_name) ⇒ Object
59 60 61 |
# File 'lib/committer/config/accessor.rb', line 59 def read_file_from_home(file_name) read_file_from_path(File.join(Committer::Config::Constants::CONFIG_DIR, file_name)) end |
#read_file_from_path(path) ⇒ Object
49 50 51 52 53 |
# File 'lib/committer/config/accessor.rb', line 49 def read_file_from_path(path) return '' unless File.exist?(path) File.read(path) end |
#read_path_prioritized_file(file_name) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/committer/config/accessor.rb', line 67 def read_path_prioritized_file(file_name) git_path_contents = read_file_from_git_root(file_name) return git_path_contents unless git_path_contents.empty? home_path_contents = read_file_from_home(file_name) return home_path_contents unless home_path_contents.empty? load_default_file(file_name) end |
#reload ⇒ Object
Force reload configuration (useful for testing)
80 81 82 |
# File 'lib/committer/config/accessor.rb', line 80 def reload @config = load_config end |
#to_h ⇒ Object
Get the entire config hash
25 26 27 |
# File 'lib/committer/config/accessor.rb', line 25 def to_h @config.dup end |