Method: JSONCop::Generator#json_parsing_template

Defined in:
lib/jsoncop/generator.rb

#json_parsing_template(model) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jsoncop/generator.rb', line 63

def json_parsing_template(model)
  model.attributes.map do |attr|
    transformer = model.transformers.select { |t| t.name == attr.name }.first
    attr_key_path = model.attr_json_hash[attr.name] || attr.name
    return unless attr_key_path
    value = "json"
    key_paths = attr_key_path.split(".")
    key_paths.each_with_index do |key, index|
      value.insert 0, "("
      value += "[\"#{key}\"] as? "
      if index == key_paths.count - 1
        if transformer
          value += "#{transformer.type})"
          value += ".flatMap(#{attr.name}JSONTransformer)"
        else
          value += "#{attr.type})"
        end
      else
        value += "[String: Any])?"
      end
    end

    "let #{attr.name} = #{value}"
  end.join(",\n\t\t\t")
end