class ByteLimit

Tracks consumed bytes against an optional maximum.

Definitions

def initialize(maximum, name: :size)

Initialize a byte limit.

Signature

parameter maximum Integer | Nil

The maximum number of bytes, or nil for no limit.

parameter name Symbol

The name used when reporting a limit violation.

Implementation

def initialize(maximum, name: :size)
	if maximum and maximum < 0
		raise ArgumentError, "Multipart limits must be non-negative!"
	end
	
	@maximum = maximum
	@name = name
	@size = 0
end

attr :size

The number of bytes consumed.

def consume(size)

Consume the given number of bytes.

Signature

parameter size Integer

The number of bytes to consume.

returns Integer

The total number of bytes consumed.

Implementation

def consume(size)
	@size += size
	
	if @maximum and @size > @maximum
		raise LimitError, "Multipart #{@name} exceeded limit of #{@maximum}!"
	end
	
	return @size
end