Sha256: e05f08bce5547620c0b0e783d13415b58c60c07c76933978d0ccc0e716669feb

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module Lurker
  module Json
    class List < Schema
      TYPE = 'type'.freeze
      ARRAY = 'array'.freeze
      ITEMS = 'items'.freeze

      def merge!(schema)
        if schema.is_a?(Array)
          schema.each { |payload| @schema[ITEMS].merge!(payload) }
        else
          @schema[ITEMS].merge!(schema)
        end
      end

      def replace!(property, schema)
        if @schema[ITEMS].is_a?(Lurker::Json::Attribute)
          @schema[ITEMS] = schema
        else
          @schema[ITEMS].replace!(property, schema)
        end
      end

      private

      def parse_schema(schema)
        @schema = {}
        initialize_properties

        return if schema.empty?

        schema = schema.dup
        if schema.is_a?(Array)
          @schema[ITEMS] = @parser.typed.parse(schema.shift)

          schema.each { |payload| @schema[ITEMS].merge!(payload) }
        else
          @schema[ITEMS] = @parser.typed.parse(schema.delete ITEMS) if schema.key?(ITEMS)
          @schema.merge!(schema)
        end

        @schema
      end

      def initialize_properties
        @schema[TYPE] ||= ARRAY
        @schema[ITEMS] ||= []
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lurker-0.6.3 lib/lurker/json/schema/list.rb