Sha256: 8305f8e9f31645bb64f01309090b06e96bffbc19c71ba591f412932819f3d999

Contents?: true

Size: 1.56 KB

Versions: 25

Compression:

Stored size: 1.56 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",
      }.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
        File.write(filepath(@type), Psych.dump(to_h))
      end

      def to_h
        {
          "schema" => schema_name,
          "change_edition" => change_editions.to_h,
        }
      end

      private

      def filepath(type)
        File.join(
          @stepmod_dir,
          "data",
          base_folder,
          schema_name,
          "#{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

25 entries across 25 versions & 1 rubygems

Version Path
stepmod-utils-0.4.13 lib/stepmod/utils/change.rb
stepmod-utils-0.4.12 lib/stepmod/utils/change.rb
stepmod-utils-0.4.11 lib/stepmod/utils/change.rb
stepmod-utils-0.4.10 lib/stepmod/utils/change.rb
stepmod-utils-0.4.9 lib/stepmod/utils/change.rb
stepmod-utils-0.4.8 lib/stepmod/utils/change.rb
stepmod-utils-0.4.7 lib/stepmod/utils/change.rb
stepmod-utils-0.4.6 lib/stepmod/utils/change.rb
stepmod-utils-0.4.5 lib/stepmod/utils/change.rb
stepmod-utils-0.4.2 lib/stepmod/utils/change.rb
stepmod-utils-0.4.1 lib/stepmod/utils/change.rb
stepmod-utils-0.4.0 lib/stepmod/utils/change.rb
stepmod-utils-0.3.37 lib/stepmod/utils/change.rb
stepmod-utils-0.3.36 lib/stepmod/utils/change.rb
stepmod-utils-0.3.35 lib/stepmod/utils/change.rb
stepmod-utils-0.3.34 lib/stepmod/utils/change.rb
stepmod-utils-0.3.33 lib/stepmod/utils/change.rb
stepmod-utils-0.3.32 lib/stepmod/utils/change.rb
stepmod-utils-0.3.31 lib/stepmod/utils/change.rb
stepmod-utils-0.3.30 lib/stepmod/utils/change.rb