Sha256: 735523c2362c6b0b29492474107227bc67a3c348932a665699269186b3535dfb

Contents?: true

Size: 803 Bytes

Versions: 1

Compression:

Stored size: 803 Bytes

Contents

# frozen_string_literal: true

class OAPI::Types::Map < OAPI::Types::Object
  attr_reader :store

  def initialize(store = {}, &)
    @store = store
    super(&)
  end

  class << self
    attr_reader :item_name, :item_type

    def item(name, type = nil)
      @item_name = name
      @item_type = type

      define_method(name) do |key, ref: nil, &block|
        raise ArgumentError, "'#{key}' already exists" if @store.include?(key.to_s)

        raise ArgumentError, "ref and block are mutual exclusive" if ref && block

        return @store[key] = OAPI::Ref.new(ref) if ref

        @store[key] = type.new(&block)
      end
    end

    def parse(json)
      store = json.transform_values do |value|
        item_type ? item_type.parse(value) : value
      end
      new(store)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oapi-0.1.2 lib/oapi/types/map.rb