Class: Protocol::Content::Parameters::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/content/parameters/builder.rb

Overview

Builds immutable parameter models using a field DSL.

Instance Method Summary collapse

Constructor Details

#initialize(parser: Parser.default, types: TYPES, strict: true) ⇒ Builder

Initialize a parameter model builder.



17
18
19
20
21
22
# File 'lib/protocol/content/parameters/builder.rb', line 17

def initialize(parser: Parser.default, types: TYPES, strict: true)
	@parser = parser
	@types = types
	@strict = strict
	@fields = {}
end

Instance Method Details

#array(name, type = nil, required: false, nullable: false, strict: @strict, &block) ⇒ Object

Declare an array of scalar values or nested argument hierarchies.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/protocol/content/parameters/builder.rb', line 77

def array(name, type = nil, required: false, nullable: false, strict: @strict, &block)
	if block
		# A block defines the element shape and cannot be combined with conversion:
		if type
			raise ArgumentError, "An array cannot declare both an element type and nested fields!"
		end
		
		model = nested_model(strict:, &block)
	elsif type
		type = resolve(type)
	end
	
	return add(ArrayField.new(name, type, model, required:, nullable:))
end

#build(&block) ⇒ Object

Evaluate fields and construct an immutable parameter model.



27
28
29
30
# File 'lib/protocol/content/parameters/builder.rb', line 27

def build(&block)
	instance_eval(&block)
	return Model.new(@parser, @fields, strict: @strict).freeze
end

#enumeration(*values, **options) ⇒ Object

Construct an enumeration converter from accepted values or an input-to-output mapping.



65
66
67
# File 'lib/protocol/content/parameters/builder.rb', line 65

def enumeration(*values, **options)
	return Enumeration.build(*values, **options)
end

#field(name, type = Object, required: false, nullable: false) ⇒ Object

Declare a scalar field.



38
39
40
# File 'lib/protocol/content/parameters/builder.rb', line 38

def field(name, type = Object, required: false, nullable: false)
	return add(ValueField.new(name, resolve(type), required:, nullable:))
end

#nested(name, required: false, nullable: false, strict: @strict, &block) ⇒ Object

Declare a nested argument hierarchy. Without a block, all nested values are accepted.



99
100
101
102
103
104
105
# File 'lib/protocol/content/parameters/builder.rb', line 99

def nested(name, required: false, nullable: false, strict: @strict, &block)
	if block
		model = nested_model(strict:, &block)
	end
	
	return add(NestedField.new(name, model, required:, nullable:))
end

#upload(name, required: false, multiple: false, accept: nil, size_limit: nil) ⇒ Object

Declare a streaming file upload.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/protocol/content/parameters/builder.rb', line 49

def upload(name, required: false, multiple: false, accept: nil, size_limit: nil)
	if size_limit && size_limit < 0
		raise ArgumentError, "Upload size limit must be non-negative!"
	end
	
	if accept
		accept = Protocol::Media::Set.for(accept)
	end
	
	return add(UploadField.new(name, required:, multiple:, accept:, size_limit:))
end