lib/stepmod/utils/change.rb in stepmod-utils-0.4.14 vs lib/stepmod/utils/change.rb in stepmod-utils-0.5.0

- old
+ new

@@ -3,25 +3,32 @@ require "stepmod/utils/change_edition_collection" module Stepmod module Utils class Change - attr_accessor :schema_name + attr_accessor :schema_name, :mapping_table attr_reader :change_editions MODULE_TYPES = { arm: "arm", mim: "mim", arm_longform: "arm_lf", mim_longform: "mim_lf", mapping: "mapping", - changes: "", + changes: "changes", + mapping_table: "mapping", }.freeze + TYPES_WITHOUT_EXTENSION = %w[ + changes + mapping_table + ].freeze + def initialize(stepmod_dir:, schema_name:, type:) @stepmod_dir = stepmod_dir @change_editions = Stepmod::Utils::ChangeEditionCollection.new + @mapping_table = {} @schema_name = schema_name @type = type end def resource? @@ -40,14 +47,18 @@ @change_editions[version] end alias_method :[], :fetch_change_edition def save_to_file - change_hash = to_h + change_hash = if @type == "mapping_table" + @mapping_table + else + to_h + end return if change_hash.empty? - File.write(filepath(@type), Psych.dump(change_hash)) + File.write(filepath(@type), Psych.dump(stringify_keys(change_hash))) end def to_h change_editions_list = change_editions.to_h @@ -70,20 +81,37 @@ filename(type), ) end def filename(type) - return "changes.yaml" if type.to_s == "changes" + return "#{MODULE_TYPES[type.to_sym]}.yaml" if TYPES_WITHOUT_EXTENSION.include?(type.to_s) "#{MODULE_TYPES[type.to_sym] || schema_name}.changes.yaml" end def base_folder if resource? "resources" else "modules" end + end + + # Hash#transform_keys is not available in Ruby 2.4 + # so we have to do this ourselves :( + # symbolize hash keys + def stringify_keys(hash) + result = {} + hash.each_pair do |key, value| + result[key.to_s] = if value.is_a?(Hash) + stringify_keys(value) + elsif value.is_a?(Array) + value.map { |v| stringify_keys(v) } + else + value + end + end + result end end end end