Class: ArtifactTools::ConfigFile
- Inherits:
-
Object
- Object
- ArtifactTools::ConfigFile
- Includes:
- Hasher
- Defined in:
- lib/artifact_tools/config_file.rb
Overview
Store configuration information about artifacts and where they are stored.
It has to contain at least the fields from REQUIRED_FIELDS while allowing any key/value which has a value for the user.
Constant Summary collapse
- REQUIRED_FIELDS =
%w[server dir files].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
-
.from_file(file) ⇒ Object
Create ConfigFile from file in YAML format.
Instance Method Summary collapse
-
#append_file(file:, store_path: nil, **opts) ⇒ Object
Append file to configuration.
-
#initialize(config:) ⇒ ConfigFile
constructor
Initialize config file.
-
#save(file) ⇒ Object
Saves configuration to file.
Methods included from Hasher
Constructor Details
#initialize(config:) ⇒ ConfigFile
Initialize config file
20 21 22 23 24 25 26 |
# File 'lib/artifact_tools/config_file.rb', line 20 def initialize(config:) raise 'Invalid config' unless REQUIRED_FIELDS.all? { |k| config.keys.include?(k) } raise 'Invalid config' unless [NilClass, Hash].any? { |klass| config['files'].is_a?(klass) } @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/artifact_tools/config_file.rb', line 13 def config @config end |
Class Method Details
.from_file(file) ⇒ Object
Create ConfigFile from file in YAML format
31 32 33 34 |
# File 'lib/artifact_tools/config_file.rb', line 31 def self.from_file(file) ConfigFile.new(config: YAML.load_file(file)) # Leave error propagation as this is development tool end |
Instance Method Details
#append_file(file:, store_path: nil, **opts) ⇒ Object
Note:
If file exists in the config with key store_path then its properties will be merged, where new ones will have priority.
Append file to configuration.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/artifact_tools/config_file.rb', line 52 def append_file(file:, store_path: nil, **opts) store_path ||= file # Convert symbols to String opts = hash_keys_to_strings(opts) @config['files'] ||= {} @config['files'][store_path] = opts @config['files'][store_path]['hash'] ||= file_hash(file) end |
#save(file) ⇒ Object
Saves configuration to file
39 40 41 42 |
# File 'lib/artifact_tools/config_file.rb', line 39 def save(file) File.write(file, @config.to_yaml) # Leave error propagation as this is development tool end |