Method: Praxis::Types::MultipartArray#push

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

#push(*parts, context: Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object

Raises:

  • (Attributor::AttributorException)


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/praxis/types/multipart_array.rb', line 237

def push(*parts, context: Attributor::DEFAULT_ROOT_CONTEXT)
  part, *rest = parts
  if rest.any?
    return self.push(part, context: context).push(*rest, context:context)
  end

  original_context = context

  part.name = self.class.name_type.load(part.name, self.class.generate_subcontext(context, part.name))
  key = part.name

  context = self.class.generate_subcontext(context, key)

  # If no attributes are defined, we always use the default
  # payload_attribute, otherwise we constrain the parts
  # to the defined names.
  attribute = if self.class.attributes.empty?
    self.class.part_attribute
  elsif (default_thingy = self.class.attributes[key])
    default_thingy
  else
    nil
  end

  if attribute
    part.attribute = attribute
    part.load_payload(context + ['payload'])
    part.load_headers(context + ['headers'])
    return self << part
  elsif self.class.options[:case_insensitive_load]
    name = self.class.attributes.keys.find do |k|
      k.kind_of?(String) && key.downcase == k.downcase
    end
    if name
      part.name = name
      return self.push(part, context: original_context)
    end
  end

  raise Attributor::AttributorException, "Unknown part name received: #{key.inspect} while loading #{Attributor.humanize_context(context)}"
end