Sha256: 9e8bbe5cac0373371f28d5aad9ab1f9abd7f3c66f8038675262e02a1b7a2e7c2

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

require_relative '../pathname_manager'
require 'r2-oas/schema/v3/base'

module R2OAS
  module Schema
    module V3
      class BaseFileManager < Base
        attr_accessor :original_path

        # e.x.) openapi_path = "#/components/schemas/Account"
        def initialize(path, path_type = :full)
          super()
          @ext_name = :yml
          @path_type = path_type
          @original_path = path
          @relative_save_file_path = PathnameManager.new(path, path_type).relative_save_file_path
        end

        def delete
          File.delete(save_file_path) if FileTest.exists?(save_file_path)
        end

        def save(data)
          File.write(save_file_path, data)
        end

        def save_after_deep_merge(data)
          result = load_data.deep_merge(data)
          save(result.to_yaml)
        end

        def save_file_path
          File.expand_path(@relative_save_file_path).tap do |abs_path|
            abs_dir = File.dirname(abs_path)
            FileUtils.mkdir_p(abs_dir) unless FileTest.exists?(abs_dir)
          end
        end

        def load_data
          case @ext_name
          when :yml
            if FileTest.exists?(save_file_path)
              YAML.load_file(save_file_path)
            else
              {}
            end
          else
            raise NoSupportError, "Do not support @ext_name: #{@ext_name}"
          end
        end

        def descendants_paths
          []
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
r2-oas-0.1.2 lib/r2-oas/schema/v3/manager/file/base_file_manager.rb
r2-oas-0.1.0 lib/r2-oas/schema/v3/manager/file/base_file_manager.rb