Class: GDK::ConfigSettings

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gdk/config_settings.rb

Direct Known Subclasses

Config, GDK::ConfigExample::Settings

Defined Under Namespace

Modules: Persisted

Constant Summary collapse

SettingUndefined =
Class.new(StandardError)
UnsupportedConfiguration =
Class.new(StandardError)
LooseFile =
Class.new(StandardError)
YamlModified =
Class.new(StandardError)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml: {}, key: nil, parent: nil) ⇒ ConfigSettings

Returns a new instance of ConfigSettings.



81
82
83
84
85
# File 'lib/gdk/config_settings.rb', line 81

def initialize(yaml: {}, key: nil, parent: nil)
  @yaml = yaml
  @key = key
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object

Provide a shorter form for ‘config.setting.enabled` as `config.setting?`



213
214
215
216
217
218
219
# File 'lib/gdk/config_settings.rb', line 213

def method_missing(method_name, *args, &blk)
  enabled = enabled_value(method_name)

  return super if enabled.nil?

  enabled
end

Class Attribute Details

.attributesObject

Returns the value of attribute attributes.



21
22
23
# File 'lib/gdk/config_settings.rb', line 21

def attributes
  @attributes
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



16
17
18
# File 'lib/gdk/config_settings.rb', line 16

def key
  @key
end

#parentObject (readonly)

Returns the value of attribute parent.



16
17
18
# File 'lib/gdk/config_settings.rb', line 16

def parent
  @parent
end

#yamlObject (readonly)

Returns the value of attribute yaml.



16
17
18
# File 'lib/gdk/config_settings.rb', line 16

def yaml
  @yaml
end

Class Method Details

.anything(key, &blk) ⇒ Object



27
28
29
# File 'lib/gdk/config_settings.rb', line 27

def anything(key, &blk)
  def_attribute(key, ConfigType::Anything, &blk)
end

.array(key, merge: false, &blk) ⇒ Object



31
32
33
# File 'lib/gdk/config_settings.rb', line 31

def array(key, merge: false, &blk)
  def_attribute(key, ConfigType::Array, merge: merge, &blk)
end

.bool(key, &blk) ⇒ Object



39
40
41
42
# File 'lib/gdk/config_settings.rb', line 39

def bool(key, &blk)
  def_attribute(key, ConfigType::Bool, &blk)
  alias_method :"#{key}?", key
end

.hash_setting(key, merge: false, &blk) ⇒ Object



35
36
37
# File 'lib/gdk/config_settings.rb', line 35

def hash_setting(key, merge: false, &blk)
  def_attribute(key, ConfigType::Hash, merge: merge, &blk)
end

.integer(key, &blk) ⇒ Object



44
45
46
# File 'lib/gdk/config_settings.rb', line 44

def integer(key, &blk)
  def_attribute(key, ConfigType::Integer, &blk)
end

.load_from_fileObject



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

def load_from_file
  Persisted.new(self)
end

.path(key, &blk) ⇒ Object



52
53
54
# File 'lib/gdk/config_settings.rb', line 52

def path(key, &blk)
  def_attribute(key, ConfigType::Path, &blk)
end

.port(key, service_name, &blk) ⇒ Object



48
49
50
# File 'lib/gdk/config_settings.rb', line 48

def port(key, service_name, &blk)
  def_attribute(key, ConfigType::Port, service_name: service_name, &blk)
end

.settings(key, &blk) ⇒ Object



60
61
62
# File 'lib/gdk/config_settings.rb', line 60

def settings(key, &blk)
  def_attribute(key, ConfigType::Settings, &blk)
end

.settings_array(key, size: nil, &blk) ⇒ Object



64
65
66
# File 'lib/gdk/config_settings.rb', line 64

def settings_array(key, size: nil, &blk)
  def_attribute(key, ConfigType::SettingsArray, size: size, &blk)
end

.string(key, &blk) ⇒ Object



56
57
58
# File 'lib/gdk/config_settings.rb', line 56

def string(key, &blk)
  def_attribute(key, ConfigType::String, &blk)
end

Instance Method Details

#[](slug) ⇒ Object



150
151
152
# File 'lib/gdk/config_settings.rb', line 150

def [](slug)
  fetch(slug, nil)
end

#bury!(*slugs, new_value) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/gdk/config_settings.rb', line 164

def bury!(*slugs, new_value)
  slugs = slugs.first.to_s.split('.') if slugs.one?
  key = slugs.shift

  if slugs.empty?
    setting = build(key)
    setting.value = new_value
    yaml[key] = setting.value # Sanitize
  else
    fetch(key).bury!(*slugs, new_value)
  end
end

#bury_multiple!(key_value_pairs) ⇒ Object



177
178
179
180
181
# File 'lib/gdk/config_settings.rb', line 177

def bury_multiple!(key_value_pairs)
  key_value_pairs.each do |key, value|
    bury!(key, value)
  end
end

#config_file_protected?(target) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
# File 'lib/gdk/config_settings.rb', line 183

def config_file_protected?(target)
  return false if gdk.overwrite_changes

  gdk.protected_config_files&.any? { |pattern| File.fnmatch(pattern, target) }
end

#dig(*slugs) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/gdk/config_settings.rb', line 154

def dig(*slugs)
  slugs = slugs.first.to_s.split('.') if slugs.one?

  value = fetch(slugs.shift)

  return value if slugs.empty?

  value.dig(*slugs)
end

#dump!(user_only: false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gdk/config_settings.rb', line 97

def dump!(user_only: false)
  attributes.values.sort_by(&:key).each_with_object({}) do |attribute, result|
    # We don't dump a config if it:
    #  - starts with a double underscore (intended for internal use)
    #  - is a ? method (always has a non-? counterpart)
    next if attribute.ignore?

    attr_value = attribute.build(parent: self)
    next if user_only && !attr_value.user_defined?

    result[attribute.key] = attr_value.dump!(user_only: user_only)
  end
end

#dump_as_yamlObject



111
112
113
# File 'lib/gdk/config_settings.rb', line 111

def dump_as_yaml
  dump!.to_yaml
end

#fetch(slug, *args) ⇒ Object

Raises:

  • (::ArgumentError)


140
141
142
143
144
145
146
147
148
# File 'lib/gdk/config_settings.rb', line 140

def fetch(slug, *args)
  raise ::ArgumentError, %[Wrong number of arguments (#{args.count + 1} for 1..2)] if args.count > 1

  return public_send(slug) if respond_to?(slug) # rubocop:disable GitlabSecurity/PublicSend

  raise SettingUndefined, %(Could not fetch the setting '#{slug}' in '#{self.slug || '<root>'}') if args.empty?

  args.first
end

#find_executable!(bin) ⇒ Object



115
116
117
# File 'lib/gdk/config_settings.rb', line 115

def find_executable!(bin)
  Utils.find_executable(bin)
end

#inspectObject



200
201
202
203
204
# File 'lib/gdk/config_settings.rb', line 200

def inspect
  return "#<#{self.class.name}>" if self.class.name

  "#<GDK::ConfigSettings slug:#{slug}>"
end

#port_managerObject



229
230
231
232
233
234
# File 'lib/gdk/config_settings.rb', line 229

def port_manager
  # Only the root should hold the PortManager
  return root.port_manager if parent

  @port_manager ||= PortManager.new_from_config(config: self)
end

#read!(filename) ⇒ Object

Raises:



119
120
121
122
123
# File 'lib/gdk/config_settings.rb', line 119

def read!(filename)
  return unless File.exist?(filename)

  raise LooseFile, "Loose file '#{filename}' is no longer supported."
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/gdk/config_settings.rb', line 221

def respond_to_missing?(method_name, include_private = false)
  !enabled_value(method_name).nil? || super
end

#rootObject Also known as: config



195
196
197
# File 'lib/gdk/config_settings.rb', line 195

def root
  parent&.root || self
end

#settings_klassObject



225
226
227
# File 'lib/gdk/config_settings.rb', line 225

def settings_klass
  ::GDK::ConfigSettings
end

#slugObject



189
190
191
192
193
# File 'lib/gdk/config_settings.rb', line 189

def slug
  return nil unless parent

  [parent.slug, key].compact.join('.')
end

#to_sObject



206
207
208
# File 'lib/gdk/config_settings.rb', line 206

def to_s
  dump!.to_yaml
end

#user_defined?(*slugs) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gdk/config_settings.rb', line 125

def user_defined?(*slugs)
  if slugs.any?
    slugs = slugs.first.to_s.split('.') if slugs.one?
    key = slugs.shift

    return build(key).user_defined?(*slugs)
  end

  attributes.values.any? do |attribute|
    next if attribute.ignore?

    attribute.build(parent: self).user_defined?
  end
end

#validate!Object



87
88
89
90
91
92
93
94
95
# File 'lib/gdk/config_settings.rb', line 87

def validate!
  attributes.each_value do |attribute|
    next if attribute.ignore?

    attribute.build(parent: self).validate!
  end

  nil
end