Class: Lurker::Json::Parser::TypedStrategy

Inherits:
Object
  • Object
show all
Includes:
Expertise
Defined in:
lib/lurker/json/parser/typed_strategy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Expertise

type_defined?, type_polymorph?, type_supposed?

Constructor Details

#initialize(options) ⇒ TypedStrategy

Returns a new instance of TypedStrategy.



9
10
11
12
13
14
# 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

Instance Attribute Details

#schema_optionsObject (readonly)

Returns the value of attribute schema_options.



7
8
9
# File 'lib/lurker/json/parser/typed_strategy.rb', line 7

def schema_options
  @schema_options
end

Instance Method Details

#parse(payload) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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