class Lurker::Json::Parser::TypedStrategy

Attributes

schema_options[R]

Public Class Methods

new(options) click to toggle source
# File lib/lurker/json/parser/typed_strategy.rb, line 9
def initialize(options)
  options = options.dup

  @polymorph_if_empty = options.delete(:polymorph_if_empty)
  @schema_options = options
end

Public Instance Methods

parse(payload) click to toggle source
# File lib/lurker/json/parser/typed_strategy.rb, line 16
def parse(payload)
  case payload
  when Lurker::Json::Schema
    payload
  when Hash
    return create_by_type(payload) if type_defined?(payload)
    return create_by_supposition(payload) if type_supposed?(payload)
    return create_polymorph(payload) if polymorph_if_empty? && type_polymorph?(payload)

    Lurker::Json::Object.new(payload, schema_options)
  when Array
    return create_polymorph(payload) if polymorph_if_empty? && type_polymorph?(payload)

    Lurker::Json::List.new(payload, schema_options)
  else
    Lurker::Json::Attribute.new(payload, schema_options)
  end
end

Private Instance Methods

create_by_supposition(payload) click to toggle source
# File lib/lurker/json/parser/typed_strategy.rb, line 37
def create_by_supposition(payload)
  if payload.key?(Json::ITEMS)
    Lurker::Json::List.new(payload, schema_options)
  elsif payload.key?(Json::PROPERTIES)
    Lurker::Json::Object.new(payload, schema_options)
  elsif payload.key?(Json::ANYOF)
    Lurker::Json::Tuple::AnyOf.new(payload, schema_options)
  elsif payload.key?(Json::ALLOF)
    Lurker::Json::Tuple::AllOf.new(payload, schema_options)
  elsif payload.key?(Json::ONEOF)
    Lurker::Json::Tuple::OneOf.new(payload, schema_options)
  elsif payload.key?(Json::REF)
    Lurker::Json::Reference.new(payload, schema_options)
  else
    raise "Unknown type supposition for #{payload}"
  end
end
create_by_type(payload) click to toggle source
# File lib/lurker/json/parser/typed_strategy.rb, line 55
def create_by_type(payload)
  case payload[Json::TYPE]
  when Json::OBJECT
    Lurker::Json::Object.new(payload, schema_options)
  when Json::ARRAY
    Lurker::Json::List.new(payload, schema_options)
  else
    Lurker::Json::Attribute.new(payload, schema_options)
  end
end
create_polymorph(payload) click to toggle source
# File lib/lurker/json/parser/typed_strategy.rb, line 70
def create_polymorph(payload)
  Lurker::Json::Polymorph.new(payload, schema_options)
end
polymorph_if_empty?() click to toggle source
# File lib/lurker/json/parser/typed_strategy.rb, line 66
def polymorph_if_empty?
  @polymorph_if_empty
end