class Lurker::Json::Polymorph

Public Instance Methods

merge!(schema) click to toggle source
# File lib/lurker/json/schema/polymorph.rb, line 4
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
replace!(property, schema) click to toggle source
# File lib/lurker/json/schema/polymorph.rb, line 23
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 Instance Methods

parse_schema(schema) click to toggle source
# File lib/lurker/json/schema/polymorph.rb, line 41
def parse_schema(schema)
  @schema = schema
end
subschema_options() click to toggle source

NOTE : The parser will ref to parent_schema instead

# File lib/lurker/json/schema/polymorph.rb, line 46
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