Sha256: 8431e726bd90076c2128b719962e77b459fd4de370f49dea6b541e883cafd752

Contents?: true

Size: 1.18 KB

Versions: 13

Compression:

Stored size: 1.18 KB

Contents

module ApiRegulator
  class SharedSchema
    attr_reader :name, :description, :params

    def initialize(name, description, &block)
      @name = name
      @description = description
      @params = []
      instance_eval(&block) if block_given?
    end

    def param(name, type = nil, item_type: nil, desc: "", location: :body, **options, &block)
      param = Param.new(name, type, item_type: item_type, desc: desc, location: location, **options, &block)
      @params << param
    end
  end

  @shared_schemas = {}
  @shared_schema_registry = {}

  class << self
    attr_accessor :shared_schema_registry

    def shared_schemas
      shared_schema_registry.each do |name, (description, block)|
        @shared_schemas[name] = SharedSchema.new(name, description, &block)
      end

      @shared_schemas
    end

    def shared_schema(name)
      if shared_schema_registry[name]
        description, block = shared_schema_registry.delete(name)

        @shared_schemas[name] = SharedSchema.new(name, description, &block)
      end

      @shared_schemas[name]
    end

    def register_shared_schema(name, description, &block)
      shared_schema_registry[name] = [description, block]
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
api-regulator-0.1.14 lib/api_regulator/shared_schema.rb
api-regulator-0.1.13 lib/api_regulator/shared_schema.rb
api-regulator-0.1.12 lib/api_regulator/shared_schema.rb
api-regulator-0.1.11 lib/api_regulator/shared_schema.rb
api-regulator-0.1.10 lib/api_regulator/shared_schema.rb
api-regulator-0.1.9 lib/api_regulator/shared_schema.rb
api-regulator-0.1.8 lib/api_regulator/shared_schema.rb
api-regulator-0.1.7 lib/api_regulator/shared_schema.rb
api-regulator-0.1.6 lib/api_regulator/shared_schema.rb
api-regulator-0.1.5 lib/api_regulator/shared_schema.rb
api-regulator-0.1.4 lib/api_regulator/shared_schema.rb
api-regulator-0.1.3 lib/api_regulator/shared_schema.rb
api-regulator-0.1.2 lib/api_regulator/shared_schema.rb