Class: HDOC::Configuration
- Inherits:
-
Object
- Object
- HDOC::Configuration
- Defined in:
- lib/1hdoc/core/configuration.rb
Overview
Provides methods for read configuration options by a file. By default, it uses YAML as configuration file’s parser.
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.init(file_path, file_parser = YAML, **options) ⇒ Object
Initializes a new configuration file with a set of defaults options:.
Instance Method Summary collapse
-
#initialize(file_path, file_parser = YAML) ⇒ Configuration
constructor
A new instance of Configuration.
- #load_options ⇒ Object
-
#set(option, value) ⇒ Object
Sets a new option or a edit an existing one.
-
#update ⇒ Object
Updates configuration file by rewriting the whole file using init’s singleton method.
Constructor Details
#initialize(file_path, file_parser = YAML) ⇒ Configuration
Returns a new instance of Configuration.
25 26 27 28 29 |
# File 'lib/1hdoc/core/configuration.rb', line 25 def initialize(file_path, file_parser = YAML) @file_path = File.(file_path) @file_parser = file_parser = end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
6 7 8 |
# File 'lib/1hdoc/core/configuration.rb', line 6 def file_path @file_path end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/1hdoc/core/configuration.rb', line 6 def end |
Class Method Details
.init(file_path, file_parser = YAML, **options) ⇒ Object
Initializes a new configuration file with a set of defaults options:
“‘ # Using a custom format Configuration.init(’~/package.json’, JSON, name: ‘howsweather’, version: ‘0.1.4’)
# Using default format Configuration.init(‘~/.1hdoc.yml’, day: 0, last_commit_on: Time.now) “‘
18 19 20 21 22 23 |
# File 'lib/1hdoc/core/configuration.rb', line 18 def self.init(file_path, file_parser = YAML, **) file_path = File.(file_path) = file_parser.dump() File.open(file_path, 'w') { |config| config.puts() } end |
Instance Method Details
#load_options ⇒ Object
31 32 33 34 |
# File 'lib/1hdoc/core/configuration.rb', line 31 def raise "Unable to find #{file_path}" unless File.exist? file_path @file_parser.load(File.read(file_path)) end |
#set(option, value) ⇒ Object
Sets a new option or a edit an existing one.
It’s used in pair with Configuration#update in order to update an existing configuration file.
50 51 52 |
# File 'lib/1hdoc/core/configuration.rb', line 50 def set(option, value) [option] = value end |
#update ⇒ Object
Updates configuration file by rewriting the whole file using init’s singleton method.
You should avoid this method for big files and use it for small ones.
41 42 43 |
# File 'lib/1hdoc/core/configuration.rb', line 41 def update self.class.init(@file_path, @file_parser, ) end |