Class: ArtifactTools::ConfigFile

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hasher

#file_hash

Constructor Details

#initialize(config:) ⇒ ConfigFile

Initialize config file

Parameters:

  • config (Hash)

    Provide configuration. Mandatory fields are REQUIRED_FIELDS



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

#configObject (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

Parameters:

  • file (String)

    Path to 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.

Parameters:

  • file (String)

    Path to the file to store in the configuration

  • store_path (String) (defaults to: nil)

    Use this path as key in the configuration. Optional, if omitted uses file

  • opts (Hash)

    Additional fields to store for the file



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

Parameters:

  • file (String)

    Save in this file. Overwrites the file if present.



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