Class: Meta::RouteDSL::MetaBuilder

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

Defined Under Namespace

Modules: Delegator

Instance Method Summary collapse

Constructor Details

#initialize(route_full_path:, route_method: :all, &block) ⇒ MetaBuilder

Returns a new instance of MetaBuilder.



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

def initialize(route_full_path:, route_method: :all, &block)
  @route_full_path = route_full_path
  @method = route_method
  @meta = { scope: [] }
  @parameters_builder = ParametersBuilder.new(route_full_path: route_full_path, route_method: route_method) # 默认给一个空的参数构建器,它只会处理 path 参数

  instance_exec &block if block_given?
end

Instance Method Details

#buildObject



18
19
20
21
22
23
24
# File 'lib/meta/route_dsl/meta_builder.rb', line 18

def build
  meta = @meta
  if @meta[:parameters].nil? && @route_full_path =~ /[:*].+/
    meta[:parameters] = ParametersBuilder.new(route_full_path: @route_full_path, route_method: @method).build
  end
  meta
end

#parameters(&block) ⇒ Object



26
27
28
# File 'lib/meta/route_dsl/meta_builder.rb', line 26

def parameters(&block)
  @meta[:parameters] = ParametersBuilder.new(route_full_path: @route_full_path, route_method: @method, &block).build
end

#params(&block) ⇒ Object

params 宏是一个遗留的宏,它在一个宏定义块内同时定义 parameters 和 request_body



35
36
37
# File 'lib/meta/route_dsl/meta_builder.rb', line 35

def params(&block)
  @meta[:parameters], @meta[:request_body] = UniformedParamsBuilder.new(route_full_path: @route_full_path, route_method: @method, &block).build
end

#request_body(options = {}, &block) ⇒ Object



30
31
32
# File 'lib/meta/route_dsl/meta_builder.rb', line 30

def request_body(options = {}, &block)
  @meta[:request_body] = JsonSchema::SchemaBuilderTool.build(options, &block).to_schema
end

#scope(scope) ⇒ Object



46
47
48
49
50
# File 'lib/meta/route_dsl/meta_builder.rb', line 46

def scope(scope)
  scope = [scope] unless scope.is_a?(Array)

  @meta[:scope] = scope
end

#status(code, *other_codes, **options, &block) ⇒ Object



39
40
41
42
43
44
# File 'lib/meta/route_dsl/meta_builder.rb', line 39

def status(code, *other_codes, **options, &block)
  codes = [code, *other_codes]
  entity_schema = JsonSchema::SchemaBuilderTool.build(options, &block)
  @meta[:responses] = @meta[:responses] || {}
  codes.each { |code| @meta[:responses][code] = entity_schema }
end