Class: Bauk::Gen::Configs::Base
- Inherits:
-
Object
- Object
- Bauk::Gen::Configs::Base
- Includes:
- Utils::Log
- Defined in:
- lib/bauk/gen/configs/base.rb
Overview
Base class for all config generators. Each child class needs to overwrite the following method:
-
generate_config() -> This returns a hash of the config it obtains
Direct Known Subclasses
Instance Method Summary collapse
-
#add_config!(config, keys = @keys) ⇒ Object
This method takes a config and adds the config that the config_generator will generate.
-
#initialize(map = {}) ⇒ Base
constructor
Takes optional options: - keys: an array of where this config will be located in the final config hash.
- #symbolize(obj) ⇒ Object
Constructor Details
#initialize(map = {}) ⇒ Base
Takes optional options:
-
keys: an array of where this config will be located in the final config hash
18 19 20 |
# File 'lib/bauk/gen/configs/base.rb', line 18 def initialize(map = {}) @keys = map[:keys] || [] end |
Instance Method Details
#add_config!(config, keys = @keys) ⇒ Object
This method takes a config and adds the config that the config_generator
will generate. It adds the generated config in a position on the hash
dependant on which keys were passed to the config generator
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bauk/gen/configs/base.rb', line 25 def add_config!(config, keys = @keys) log.debug 'adding config' return config.deep_merge! generate_config if keys.empty? key = keys.shift config[key] = {} if (config[key] == true) || !(config[key]) # config[key] = add_config(config[key], keys) add_config!(config[key], keys) config end |
#symbolize(obj) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bauk/gen/configs/base.rb', line 36 def symbolize(obj) if obj.is_a? Hash return obj.reduce({}) do |memo, (k, v)| memo.tap { |m| m[k.to_sym] = symbolize(v) } end elsif obj.is_a? Array return obj.each_with_object([]) do |v, memo| memo << symbolize(v) end end obj end |