Sha256: bc5b20c90160041064bc06f2dfec905be564ff583084b38e2ccc7a408aace3a3

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require "psych"
require "stepmod/utils/change_edition"
require "stepmod/utils/change_edition_collection"

module Stepmod
  module Utils
    class Change
      attr_accessor :schema_name
      attr_reader :change_editions

      MODULE_TYPES = {
        arm: "arm",
        mim: "mim",
        arm_longform: "arm_lf",
        mim_longform: "mim_lf",
        mapping: "mapping",
        changes: "",
      }.freeze

      def initialize(stepmod_dir:, schema_name:, type:)
        @stepmod_dir = stepmod_dir
        @change_editions = Stepmod::Utils::ChangeEditionCollection.new
        @schema_name = schema_name
        @type = type
      end

      def resource?
        !module?
      end

      def module?
        MODULE_TYPES.key?(@type.to_sym) || MODULE_TYPES.value?(@type.to_s)
      end

      def add_change_edition(change_edition)
        @change_editions[change_edition[:version]] = change_edition
      end

      def fetch_change_edition(version)
        @change_editions[version]
      end
      alias_method :[], :fetch_change_edition

      def save_to_file
        change_hash = to_h
        return if change_hash.empty?

        File.write(filepath(@type), Psych.dump(change_hash))
      end

      def to_h
        change_editions_list = change_editions.to_h

        return {} if change_editions_list.empty?

        {
          "schema" => schema_name,
          "change_edition" => change_editions_list,
        }
      end

      private

      def filepath(type)
        File.join(
          @stepmod_dir,
          "data",
          base_folder,
          schema_name,
          filename(type),
        )
      end

      def filename(type)
        return "changes.yaml" if type.to_s == "changes"

        "#{MODULE_TYPES[type.to_sym] || schema_name}.changes.yaml"
      end

      def base_folder
        if resource?
          "resources"
        else
          "modules"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stepmod-utils-0.4.14 lib/stepmod/utils/change.rb