Sha256: 866a683d8515430cc73f9cbb5093c0152ee56f1ce34c0220f31ef09a30608623

Contents?: true

Size: 1.95 KB

Versions: 9

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require 'r2-oas/schema/v3/base'

# Scope Rails
module R2OAS
  module Schema
    module V3
      class BaseAnalyzer < Base
        include Sortable

        def initialize(before_schema_data, after_schema_data, options = {})
          super(options)
          @type = options[:type].presence
          @before_schema_data = before_schema_data
          @after_schema_data  = after_schema_data.presence || create_after_schema_data
        end

        def analyze_docs
          raise NoImplementError, 'Please implement in inherited class.'
        end

        def generate_from_existing_schema
          raise NoImplementError, 'Please implement in inherited class.'
        end

        private

        attr_accessor :existing_schema_file_path
        attr_accessor :type

        def create_after_schema_data
          case @type
          when :edited
            {}
          when :existing
            if existing_schema_file_path.present?
              create_after_schema_data_when_specify_path
            else
              create_after_schema_data_when_not_specify_path
            end
          end
        end

        def create_after_schema_data_when_not_specify_path
          if FileTest.exists?(doc_save_file_path)
            YAML.load_file(doc_save_file_path)
          else
            raise NoFileExistsError, "Do not exists file: #{doc_save_file_path}"
          end
        end

        def create_after_schema_data_when_specify_path
          extname = File.extname(existing_schema_file_path)
          case extname
          when /json/
            File.open(existing_schema_file_path) do |file|
              JSON.parse(file.read)
            end
          when /yaml/
            YAML.load_file(existing_schema_file_path)
          when /yml/
            YAML.load_file(existing_schema_file_path)
          else
            raise NoImplementError, "Do not support extension: #{extname}"
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
r2-oas-0.5.0 lib/r2-oas/schema/v3/analyzer/base_analyzer.rb
r2-oas-0.4.1 lib/r2-oas/schema/v3/analyzer/base_analyzer.rb
r2-oas-0.4.0 lib/r2-oas/schema/v3/analyzer/base_analyzer.rb
r2-oas-0.3.4 lib/r2-oas/schema/v3/analyzer/base_analyzer.rb
r2-oas-0.3.3 lib/r2-oas/schema/v3/analyzer/base_analyzer.rb
r2-oas-0.3.2 lib/r2-oas/schema/v3/analyzer/base_analyzer.rb
r2-oas-0.3.1 lib/r2-oas/schema/v3/analyzer/base_analyzer.rb
r2-oas-0.3.0 lib/r2-oas/schema/v3/analyzer/base_analyzer.rb
r2-oas-0.2.0 lib/r2-oas/schema/v3/analyzer/base_analyzer.rb