Sha256: b6ef700ab57534e9178ba64b48b8e2afde1a312f4e2cbb124c9c91734ebc3526

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

module Praxis
  class Collection < Attributor::Collection
    include Types::MediaTypeCommon

    def self.of(type)
      if defined?(type::Collection)
        return type::Collection
      end

      klass = super
      klass.anonymous_type

      if type < Praxis::Types::MediaTypeCommon
        klass.member_type type
        type.const_set :Collection, klass
      else
        raise "Praxis::Collection.of() for non-MediaTypes is unsupported. Use Attributor::Collection.of() instead."
      end

    end

    def self.member_type(type=nil)
      unless type.nil?
        @member_type = type
        self.identifier(type.identifier + ';type=collection') unless type.identifier.nil?
      end

      @member_type
    end

    def self.domain_model
      @member_type.domain_model
    end

    def self.json_schema_type
      :array
    end

    def self.as_json_schema(**args)
      the_type = @attribute && @attribute.type || member_type
      {
        type: json_schema_type,
        items: { '$ref': "#/components/schemas/#{the_type.id}" }
      }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
praxis-2.0.pre.18 lib/praxis/collection.rb
praxis-2.0.pre.17 lib/praxis/collection.rb
praxis-2.0.pre.16 lib/praxis/collection.rb
praxis-2.0.pre.15 lib/praxis/collection.rb