Class: RubynCode::Config::ProjectConfig

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_root: Dir.pwd, global_settings: nil) ⇒ ProjectConfig

Returns a new instance of ProjectConfig.

Parameters:

  • project_root (String) (defaults to: Dir.pwd)

    the root directory of the project (defaults to pwd)

  • global_settings (Settings, nil) (defaults to: nil)

    global settings to fall back to



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.expand_path(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_pathObject (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

#dataObject (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_rootObject (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.expand_path(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

Returns:

  • (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.message}"
rescue SystemCallError => e
  raise LoadError, "Failed to save project config to #{@config_path}: #{e.message}"
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_hObject



61
62
63
# File 'lib/rubyn_code/config/project_config.rb', line 61

def to_h
  @global_settings.to_h.merge(@data)
end