Class: Meta::RouteDSL::ParametersBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/meta/route_dsl/parameters_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(route_full_path:, route_method:, &block) ⇒ ParametersBuilder

Returns a new instance of ParametersBuilder.



9
10
11
12
13
14
15
# File 'lib/meta/route_dsl/parameters_builder.rb', line 9

def initialize(route_full_path:, route_method:, &block)
  @route_full_path = route_full_path || ''
  @route_method = route_method
  @parameter_options = {}

  instance_exec &block if block_given?
end

Instance Method Details

#buildObject



31
32
33
34
35
36
37
38
# File 'lib/meta/route_dsl/parameters_builder.rb', line 31

def build
  # 补充未声明的 path 参数
  (path_param_names - @parameter_options.keys).each do |name|
    @parameter_options[name] = { in: 'path', schema: JsonSchema::BaseSchema.new(required: true) }
  end

  Parameters.new(@parameter_options)
end

#param(name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/meta/route_dsl/parameters_builder.rb', line 17

def param(name, options = {})
  # 修正 path 参数的选项
  options = options.dup
  if path_param_names.include?(name) # path 参数
    options = Utils::Kwargs::Helpers.fix!(options, in: 'path', required: true)
  else
    options = Utils::Kwargs::Helpers.merge_defaults!(options, in: 'query')
  end

  in_op = options.delete(:in)
  raise ArgumentError, "in 选项只能是 path, query, header, body" unless %w[path query header body].include?(in_op)
  @parameter_options[name] = { in: in_op, schema: JsonSchema::BaseSchema.new(options) }
end