Class: Committer::Config::Accessor

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/committer/config/accessor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAccessor



15
16
17
# File 'lib/committer/config/accessor.rb', line 15

def initialize
  @config = load_config
end

Class Method Details

.reloadObject

Class method for reload



93
94
95
# File 'lib/committer/config/accessor.rb', line 93

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_configObject



29
30
31
32
33
34
35
36
37
# File 'lib/committer/config/accessor.rb', line 29

def load_config
  # Load configs from both locations and merge them
  home_config = load_config_from_path(Committer::Config::Constants::CONFIG_FILE)
  git_root_config = load_config_from_git_root
  raise Committer::ConfigErrors::NotSetup if home_config.empty? && git_root_config.empty?

  # Merge configs with git root taking precedence
  home_config.merge(git_root_config)
end

#load_config_from_git_rootObject



77
78
79
80
81
82
83
84
85
# File 'lib/committer/config/accessor.rb', line 77

def load_config_from_git_root
  git_root = Committer::GitHelper.repo_root
  return {} if git_root.empty?

  git_config_file = File.join(git_root, '.committer', 'config.yml')
  load_config_from_path(git_config_file)
rescue StandardError
  {}
end

#load_config_from_path(path) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/committer/config/accessor.rb', line 39

def load_config_from_path(path)
  return {} unless File.exist?(path)

  result = YAML.load_file(path)
  raise Committer::ConfigErrors::FormatError, 'Config file must be a YAML hash' unless result.is_a?(Hash)

  result
end

#load_file_from_path(path) ⇒ Object



48
49
50
51
52
# File 'lib/committer/config/accessor.rb', line 48

def load_file_from_path(path)
  return '' unless File.exist?(path)

  File.read(path)
end

#load_formatting_rulesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/committer/config/accessor.rb', line 54

def load_formatting_rules
  git_root = Committer::GitHelper.repo_root
  unless git_root.empty?
    formatting_rules_git_path = File.join(git_root, '.committer',
                                          Committer::Config::Constants::FORMATTING_RULES_FILE_NAME)
  end

  git_path_contents = load_file_from_path(formatting_rules_git_path) if formatting_rules_git_path

  return git_path_contents unless git_path_contents.empty?

  home_path = File.join(Committer::Config::Constants::CONFIG_DIR,
                        Committer::Config::Constants::FORMATTING_RULES_FILE_NAME)

  home_path_contents = load_file_from_path(home_path)

  return home_path_contents unless home_path_contents.empty?

  default_path = File.join(Committer::Config::Constants::DEFAULT_PROMPT_PATH,
                           Committer::Config::Constants::FORMATTING_RULES_FILE_NAME)
  load_file_from_path(default_path)
end

#reloadObject

Force reload configuration (useful for testing)



88
89
90
# File 'lib/committer/config/accessor.rb', line 88

def reload
  @config = load_config
end

#to_hObject

Get the entire config hash



25
26
27
# File 'lib/committer/config/accessor.rb', line 25

def to_h
  @config.dup
end