Class: FIS::Configuration
- Inherits:
-
Object
- Object
- FIS::Configuration
- Defined in:
- lib/fis/configuration.rb
Overview
Handles the global configuration of the gem via ~/.flatiron-school/configuration.yml
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#file_full_path ⇒ Object
readonly
Returns the value of attribute file_full_path.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Instance Method Summary collapse
- #fetch(*keys) ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #read ⇒ Object
- #reread ⇒ Object
- #set(*keys) ⇒ Object
- #unset(*keys) ⇒ Object
- #write! ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
10 11 12 13 14 15 16 |
# File 'lib/fis/configuration.rb', line 10 def initialize @file_path = File.join(Dir.home, '.flatiron-school') @file_name = 'configuration.yml' @file_full_path = File.join(@file_path, @file_name) @config = read end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/fis/configuration.rb', line 8 def config @config end |
#file_full_path ⇒ Object (readonly)
Returns the value of attribute file_full_path.
8 9 10 |
# File 'lib/fis/configuration.rb', line 8 def file_full_path @file_full_path end |
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
8 9 10 |
# File 'lib/fis/configuration.rb', line 8 def file_name @file_name end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
8 9 10 |
# File 'lib/fis/configuration.rb', line 8 def file_path @file_path end |
Instance Method Details
#fetch(*keys) ⇒ Object
18 19 20 21 |
# File 'lib/fis/configuration.rb', line 18 def fetch(*keys) key = keys.pop keys.inject(@config, :fetch)[key] end |
#read ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fis/configuration.rb', line 37 def read return default unless File.exist?(@file_full_path) deep_merge( default, YAML.safe_load( File.open(@file_full_path).read, [Symbol] ) ) end |
#reread ⇒ Object
49 50 51 |
# File 'lib/fis/configuration.rb', line 49 def reread @config = read end |
#set(*keys) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/fis/configuration.rb', line 23 def set(*keys) key = keys.pop keys.inject(@config, :fetch)[key] = yield write! end |
#unset(*keys) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/fis/configuration.rb', line 30 def unset(*keys) set(*keys) do key = keys.pop keys.inject(default, :fetch)[key] end end |
#write! ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/fis/configuration.rb', line 53 def write! Dir.mkdir(@file_path, 0o700) unless Dir.exist?(@file_path) File.open(@file_full_path, 'w') do |file| yaml = deep_merge(default, @config).to_yaml file.write(yaml) end end |