Sha256: 127368812f7e3a07c16e1cfd99b1466b8d4ff8b57c7eb80c6c1e9a1fb7862f4f
Contents?: true
Size: 1.68 KB
Versions: 2
Compression:
Stored size: 1.68 KB
Contents
require 'ostruct' module ConfigVolumizer # Converts a {Hash} into a flattened {Hash} as a template for volumizing your configs module Generator class << self # Generates a flattened config out of a data hash # # See Readme for an example # # @param [Hash] data # @return [Hash] def generate(data) OpenStruct.new( env_hash: generate_env(data), mapping_hash: generate_mapping(data), ) end private def generate_env(data) data.inject({}) do |result, (key, value)| process_env_item(result, key, value) result end end def generate_mapping(data) data.inject({}) do |result, (key, value)| result[key] = process_mapping_item(value) result end end def process_mapping_item(value) case value when Array result = [] value.each_with_index do |item, index| result[index] = process_mapping_item(item) end result.uniq when Hash result = {} value.each do |key, item| result[key] = process_mapping_item(item) end result else :value end end def process_env_item(result, prefix, value) case value when Array value.each_with_index do |item, index| process_env_item(result, "#{prefix}_#{index}", item) end when Hash value.each do |key, item| process_env_item(result, "#{prefix}_#{key}", item) end else result[prefix] = value end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
config_volumizer-0.3.1 | lib/config_volumizer/generator.rb |
config_volumizer-0.3.0 | lib/config_volumizer/generator.rb |