Sha256: 65b099666e8d5a8659d393d01b69b9cd2b3e0819184100a8d35888ed7de14a3a

Contents?: true

Size: 1.59 KB

Versions: 9

Compression:

Stored size: 1.59 KB

Contents

module Lurker
  module Json
    class Polymorph < Schema
      def merge!(schema)
        case parent_schema
        when Lurker::Json::Object
          parent_schema[Json::PROPERTIES][parent_property] = @parser.typed.parse(schema)
        when Lurker::Json::List
          if schema.is_a?(Array)
            return if schema.empty?

            schema = schema.dup
            parent_schema[Json::ITEMS] = @parser.typed.parse(schema.shift)
            parent_schema.merge!(schema)
          else
            parent_schema[Json::ITEMS] = @parser.typed.parse(schema)
          end
        else
          parent_schema[parent_property] = @parser.typed.parse(schema)
        end
      end

      def replace!(property, schema)
        morph = Lurker::Json::Object.new({}, subschema_options)

        case parent_schema
        when Lurker::Json::Object
          parent_schema[Json::PROPERTIES][parent_property] = morph
          parent_schema.replace!(property, schema)
        when Lurker::Json::List
          parent_schema[Json::ITEMS] = morph
          parent_schema.replace!(property, schema)
        else
          parent_schema[parent_property] = morph
          parent_schema.replace!(property, schema)
        end
      end

      private

      def parse_schema(schema)
        @schema = schema
      end

      # NOTE : The parser will ref to parent_schema instead
      def subschema_options
        {uri: parent_schema.uri,
         root_schema: parent_schema.root? ? parent_schema : parent_schema.root_schema,
         parent_schema: parent_schema,
         parent_property: parent_property}
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lurker-1.0.0 lib/lurker/json/schema/polymorph.rb
lurker-0.6.12 lib/lurker/json/schema/polymorph.rb
lurker-0.6.11 lib/lurker/json/schema/polymorph.rb
lurker-0.6.10 lib/lurker/json/schema/polymorph.rb
lurker-0.6.9 lib/lurker/json/schema/polymorph.rb
lurker-0.6.8 lib/lurker/json/schema/polymorph.rb
lurker-0.6.7 lib/lurker/json/schema/polymorph.rb
lurker-0.6.6 lib/lurker/json/schema/polymorph.rb
lurker-0.6.5 lib/lurker/json/schema/polymorph.rb