Class: Optica::Client::Config
- Inherits:
-
Object
- Object
- Optica::Client::Config
- Defined in:
- lib/optica/client/config.rb
Overview
Handles storing and loading configurable settings for our CLI
Instance Attribute Summary collapse
-
#config_path ⇒ Object
Returns the value of attribute config_path.
-
#default_host ⇒ Object
Returns the value of attribute default_host.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(hash = {}) ⇒ Config
constructor
A new instance of Config.
- #reload ⇒ Object
- #set_all(data) ⇒ Object
- #to_h ⇒ Object
- #write! ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Config
Returns a new instance of Config.
14 15 16 |
# File 'lib/optica/client/config.rb', line 14 def initialize(hash = {}) set_all(hash) end |
Instance Attribute Details
#config_path ⇒ Object
Returns the value of attribute config_path.
7 8 9 |
# File 'lib/optica/client/config.rb', line 7 def config_path @config_path end |
#default_host ⇒ Object
Returns the value of attribute default_host.
8 9 10 |
# File 'lib/optica/client/config.rb', line 8 def default_host @default_host end |
Class Method Details
.from_file(path) ⇒ Object
10 11 12 |
# File 'lib/optica/client/config.rb', line 10 def self.from_file(path) new(config_path: path.to_s).reload end |
Instance Method Details
#reload ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/optica/client/config.rb', line 30 def reload return self unless File.exist?(config_path) data = YAML.load(File.read(config_path)).merge(config_path: config_path) set_all(data) self end |
#set_all(data) ⇒ Object
38 39 40 41 42 |
# File 'lib/optica/client/config.rb', line 38 def set_all(data) data.each do |key, val| self.public_send("#{key}=", val) end end |
#to_h ⇒ Object
18 19 20 21 22 23 |
# File 'lib/optica/client/config.rb', line 18 def to_h { config_path: config_path.to_s, default_host: default_host.to_s, } end |
#write! ⇒ Object
25 26 27 28 |
# File 'lib/optica/client/config.rb', line 25 def write! data = YAML.dump(to_h) File.write(config_path, data) end |