Class: Protocol::URL::FormData::Nested

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/url/form_data/nested.rb

Overview

Builds nested form data from names and values.

Constant Summary collapse

MAXIMUM_DEPTH =

The default maximum depth of a bracketed form name.

8

Instance Method Summary collapse

Constructor Details

#initialize(maximum_depth: MAXIMUM_DEPTH) ⇒ Nested

Initialize the nested form data.



18
19
20
21
# File 'lib/protocol/url/form_data/nested.rb', line 18

def initialize(maximum_depth: MAXIMUM_DEPTH)
	@maximum_depth = maximum_depth
	@root = {}
end

Instance Method Details

#add(name, value) ⇒ Object

Add a form data value using its bracketed name.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/protocol/url/form_data/nested.rb', line 27

def add(name, value)
	keys = Encoding.split(name)
	
	if keys.empty?
		raise ArgumentError, "Invalid form data name: #{name.inspect}!"
	end
	
	if @maximum_depth and keys.size > @maximum_depth
		raise RangeError, "Form data depth exceeded limit of #{@maximum_depth}!"
	end
	
	Encoding.assign(keys, value, @root)
	
	return self
end

#to_hObject

Convert the arguments to a nested hash.



45
46
47
# File 'lib/protocol/url/form_data/nested.rb', line 45

def to_h
	return @root
end