Class: Machinery::ConfigBase

Inherits:
Object
  • Object
show all
Defined in:
lib/config_base.rb

Overview

The defaults for machinery’s configuration are defined in the file ‘machinery_config.rb’.

Direct Known Subclasses

Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = default_config_file) ⇒ ConfigBase

Returns a new instance of ConfigBase.



25
26
27
28
29
30
# File 'lib/config_base.rb', line 25

def initialize(file = default_config_file)
  @entries = {}
  @file = file
  define_entries
  apply_custom_config(@file) if File.exist?(@file)
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



23
24
25
# File 'lib/config_base.rb', line 23

def entries
  @entries
end

#fileObject (readonly)

Returns the value of attribute file.



23
24
25
# File 'lib/config_base.rb', line 23

def file
  @file
end

Instance Method Details

#each(&block) ⇒ Object



45
46
47
# File 'lib/config_base.rb', line 45

def each(&block)
  @entries.map { |key, value| [unnormalize_key(key), value] }.each(&block)
end

#entry(key, parameters = {}) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/config_base.rb', line 37

def entry(key, parameters = {})
  key = normalize_key(key)

  @entries[key] = { value: parameters[:default], description: parameters[:description] }
   create_method(key.to_sym) { get(key) }
   create_method("#{key}=".to_sym) { |value| set(key, value) }
end

#get(key) ⇒ Object



49
50
51
52
53
54
# File 'lib/config_base.rb', line 49

def get(key)
  key = normalize_key(key)

  ensure_config_exists(key)
  @entries[key][:value]
end

#set(key, value, options = {auto_save: true}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/config_base.rb', line 56

def set(key, value, options = {auto_save: true} )
  if deprecated?(key)
    @entries.delete(key)
  else
    key = normalize_key(key)
    ensure_config_exists(key)

    # Check if data type is correct. true and false are not of the same type which makes the
    # check complex
    if value.class != @entries[key][:value].class &&
        !(boolean?(value) && boolean?(@entries[key][:value]))
      raise Machinery::Errors::MachineryError,
        "The value '#{value}' for configuration key '#{key}' is of an invalid data type."
    end

    @entries[key][:value] = value
  end

  save if options[:auto_save]
end