Class: Speedflow::Configuration

Inherits:
Hash
  • Object
show all
Defined in:
lib/speedflow/configuration.rb

Overview

Used to manage configuration in Speedflow core

Constant Summary collapse

DEFAULT_FILENAME =

Default configuration filename

'.speedflow.yml'.freeze
DEFAULTS =

Default options. Overridden by values in .speedflow.yml.

Configuration[{
version: 1,
source:  Dir.pwd,
plugins: []

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#hash_values_from_keys_schema, #replace_values, #replace_values_from_env, #replace_values_from_previous

Class Method Details

.get(override = {}) ⇒ Object

Get all configuration.

override - Hash of configuration.

Returns the full configuration.



102
103
104
105
106
107
108
# File 'lib/speedflow/configuration.rb', line 102

def self.get(override = {})
  config = self[self::DEFAULTS]
  override = self[override].stringify_keys

  config = config.read_config_files(config.config_files(override))
  config.deep_merge(override).stringify_keys
end

Instance Method Details

#config_files(override) ⇒ Object

Public: Generate list of configuration files from the override

override - the command-line options hash

Returns an Array of config files



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/speedflow/configuration.rb', line 55

def config_files(override)
  files = []
  if override.stringify_keys.key?('config')
    files = override.stringify_keys.delete('config')
    override.delete('config')
  else
    files << source(override) + File::SEPARATOR + DEFAULT_FILENAME
  end
  files = [files] unless files.is_a? Array
  files
end

#flow_trigger?(trigger) ⇒ Boolean

Public: Check trigger in flow

trigger - Trigger name.

Returns if trigger exists in flow.

Returns:



27
28
29
# File 'lib/speedflow/configuration.rb', line 27

def flow_trigger?(trigger)
  key?('flow') && self['flow'].key?(trigger.to_s)
end

#flow_triggers?Boolean

Public: Check triggers in flow

Returns if flow has triggers.

Returns:



18
19
20
# File 'lib/speedflow/configuration.rb', line 18

def flow_triggers?
  key?('flow') && !self['flow'].keys.empty?
end

#get_config_value_with_override(config_key, override) ⇒ Object

Public: Get config value with override

config_key - Configuration key. override - An Hash of override values.

Returns the value of config key or override.



46
47
48
# File 'lib/speedflow/configuration.rb', line 46

def get_config_value_with_override(config_key, override)
  override[config_key] || self[config_key] || DEFAULTS[config_key]
end

#read_config_file(file) ⇒ Object

Public: Read config file

file - File to read.

Returns a Hash.



91
92
93
94
95
# File 'lib/speedflow/configuration.rb', line 91

def read_config_file(file)
  SafeYAML.load_file(file) || {}
rescue
  raise ConfigurationFileFormat, "This file is not a valid YAML: #{file}"
end

#read_config_files(files) ⇒ Object

Public: Read all files content

files - the list of configuration file

Returns the full configuration.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/speedflow/configuration.rb', line 72

def read_config_files(files)
  config = clone

  files.each do |file|
    unless File.exist?(file)
      raise ConfigurationFileNotFound, "Unable to load config file: #{file}"
    end

    config = config.deep_merge(read_config_file(file))
  end

  config.replace_values_from_env
end

#source(override) ⇒ Object

Public: Directory of the Speedflow source folder

override - the command-line options hash

Returns the path to the Speedflow source directory.



36
37
38
# File 'lib/speedflow/configuration.rb', line 36

def source(override)
  get_config_value_with_override(:source, override)
end