Class: RubynCode::Config::ProjectConfig
- Inherits:
-
Object
- Object
- RubynCode::Config::ProjectConfig
- Defined in:
- lib/rubyn_code/config/project_config.rb
Defined Under Namespace
Classes: LoadError
Constant Summary collapse
- PROJECT_DIR_NAME =
'.rubyn-code'- CONFIG_FILENAME =
'config.yml'
Instance Attribute Summary collapse
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#project_root ⇒ Object
readonly
Returns the value of attribute project_root.
Class Method Summary collapse
-
.find_nearest(start_dir: Dir.pwd, global_settings: nil) ⇒ Object
Walks up the directory tree to find the nearest .rubyn-code/config.yml Returns nil if none is found before reaching the filesystem root.
Instance Method Summary collapse
- #get(key, default = nil) ⇒ Object
-
#initialize(project_root: Dir.pwd, global_settings: nil) ⇒ ProjectConfig
constructor
A new instance of ProjectConfig.
- #project_dir_exists? ⇒ Boolean
- #reload! ⇒ Object
- #save! ⇒ Object
- #set(key, value) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(project_root: Dir.pwd, global_settings: nil) ⇒ ProjectConfig
Returns a new instance of ProjectConfig.
20 21 22 23 24 25 26 27 |
# File 'lib/rubyn_code/config/project_config.rb', line 20 def initialize(project_root: Dir.pwd, global_settings: nil) @project_root = File.(project_root) @project_dir = File.join(@project_root, PROJECT_DIR_NAME) @config_path = File.join(@project_dir, CONFIG_FILENAME) @global_settings = global_settings || Settings.new @data = {} load! end |
Instance Attribute Details
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
16 17 18 |
# File 'lib/rubyn_code/config/project_config.rb', line 16 def config_path @config_path end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
16 17 18 |
# File 'lib/rubyn_code/config/project_config.rb', line 16 def data @data end |
#project_root ⇒ Object (readonly)
Returns the value of attribute project_root.
16 17 18 |
# File 'lib/rubyn_code/config/project_config.rb', line 16 def project_root @project_root end |
Class Method Details
.find_nearest(start_dir: Dir.pwd, global_settings: nil) ⇒ Object
Walks up the directory tree to find the nearest .rubyn-code/config.yml Returns nil if none is found before reaching the filesystem root.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rubyn_code/config/project_config.rb', line 71 def self.find_nearest(start_dir: Dir.pwd, global_settings: nil) dir = File.(start_dir) loop do candidate = File.join(dir, PROJECT_DIR_NAME, CONFIG_FILENAME) return new(project_root: dir, global_settings: global_settings) if File.exist?(candidate) parent = File.dirname(dir) break if parent == dir # filesystem root reached dir = parent end nil end |
Instance Method Details
#get(key, default = nil) ⇒ Object
29 30 31 |
# File 'lib/rubyn_code/config/project_config.rb', line 29 def get(key, default = nil) @data.fetch(key.to_s) { @global_settings.get(key, default) } end |
#project_dir_exists? ⇒ Boolean
65 66 67 |
# File 'lib/rubyn_code/config/project_config.rb', line 65 def project_dir_exists? File.directory?(@project_dir) end |
#reload! ⇒ Object
57 58 59 |
# File 'lib/rubyn_code/config/project_config.rb', line 57 def reload! load! end |
#save! ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/rubyn_code/config/project_config.rb', line 48 def save! ensure_project_directory! File.write(@config_path, YAML.dump(@data)) rescue Errno::EACCES => e raise LoadError, "Permission denied writing project config to #{@config_path}: #{e.}" rescue SystemCallError => e raise LoadError, "Failed to save project config to #{@config_path}: #{e.}" end |
#set(key, value) ⇒ Object
33 34 35 |
# File 'lib/rubyn_code/config/project_config.rb', line 33 def set(key, value) @data[key.to_s] = value end |
#to_h ⇒ Object
61 62 63 |
# File 'lib/rubyn_code/config/project_config.rb', line 61 def to_h @global_settings.to_h.merge(@data) end |