Method: Praxis::Types::MultipartArray.load

Defined in:
lib/praxis/types/multipart_array.rb

.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, content_type: nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/praxis/types/multipart_array.rb', line 116

def self.load(value, context=Attributor::DEFAULT_ROOT_CONTEXT, content_type:nil)
  return value if value.kind_of?(self) || value.nil?

  if value.kind_of?(::String) && content_type.nil?
    raise ArgumentError, "content_type is required to load values of type String for #{Attributor.type_name(self)}"
  end

  parser = Praxis::MultipartParser.new({'Content-Type' => content_type}, value)
  preamble, parts = parser.parse

  instance = self.new
  instance.push(*parts)

  instance.preamble = preamble
  instance.content_type = content_type

  instance
end