Class: Meta::RouteDSL::ApplicationBuilder

Inherits:
Object
  • Object
show all
Includes:
MetaBuilder::Delegator
Defined in:
lib/meta/route_dsl/application_builder.rb

Defined Under Namespace

Classes: BindingMeta

Instance Method Summary collapse

Constructor Details

#initialize(full_prefix = '/', &block) ⇒ ApplicationBuilder

Returns a new instance of ApplicationBuilder.



12
13
14
15
16
17
18
19
20
21
# File 'lib/meta/route_dsl/application_builder.rb', line 12

def initialize(full_prefix = '/', &block)
  @full_prefix = full_prefix
  @callbacks = []
  @error_guards = []
  @meta_builder = MetaBuilder.new(route_full_path: full_prefix)
  @mod_builders = []
  @shared_mods = []

  instance_exec &block if block_given?
end

Instance Method Details

#after(&block) ⇒ Object



71
72
73
74
75
76
# File 'lib/meta/route_dsl/application_builder.rb', line 71

def after(&block)
  @callbacks << {
    lifecycle: :after,
    proc: block
  }
end

#apply(builder, tags: nil) ⇒ Object

应用另一个模块



59
60
61
# File 'lib/meta/route_dsl/application_builder.rb', line 59

def apply(builder, tags: nil)
  @mod_builders << BindingMeta.new(builder, tags ? { tags: tags } : {})
end

#around(&block) ⇒ Object



78
79
80
81
82
83
# File 'lib/meta/route_dsl/application_builder.rb', line 78

def around(&block)
  @callbacks << {
    lifecycle: :around,
    proc: block
  }
end

#before(&block) ⇒ Object

定义模块内的公共逻辑



64
65
66
67
68
69
# File 'lib/meta/route_dsl/application_builder.rb', line 64

def before(&block)
  @callbacks << {
    lifecycle: :before,
    proc: block
  }
end

#build(meta_options: {}, callbacks: []) ⇒ Object

meta 和 callbacks 是父级传递过来的,需要合并到当前模块或子模块中。

为什么一定要动态传递 meta_options 参数?由于 OpenAPI 文档是面向路由的,parameters、request_body、 responses 都存在于路由文档中,对应地 Metadata 对象最终只存在于路由文档中。因此,在构建过程中,需要将父 级传递过来的 Metadata 对象合并到当前模块,再层层合并到子模块。



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/meta/route_dsl/application_builder.rb', line 28

def build(meta_options: {}, callbacks: [])
  meta_options = Utils::RouteDSLBuilders.merge_meta_options(meta_options, @meta_builder.build)
  callbacks = Utils::RouteDSLBuilders.merge_callbacks(callbacks, @callbacks)
  mods = @mod_builders.map { |builder| builder.build(meta_options: meta_options, callbacks: callbacks) }

  Application.new(
    prefix: @full_prefix,
    mods: mods,
    shared_mods: @shared_mods,
    error_guards: @error_guards
  )
end

#meta(&block) ⇒ Object

定义应用到子模块的公共逻辑



90
91
92
# File 'lib/meta/route_dsl/application_builder.rb', line 90

def meta(&block)
  @meta_builder.instance_exec &block
end

#namespace(path, &block) ⇒ Object

定义子模块



54
55
56
# File 'lib/meta/route_dsl/application_builder.rb', line 54

def namespace(path, &block)
  @mod_builders << ApplicationBuilder.new(path, &block)
end

#rescue_error(error_class, &block) ⇒ Object



85
86
87
# File 'lib/meta/route_dsl/application_builder.rb', line 85

def rescue_error(error_class, &block)
  @error_guards << { error_class: error_class, caller: block }
end

#route(path, method = nil, &block) ⇒ Object

定义路由块



47
48
49
50
51
# File 'lib/meta/route_dsl/application_builder.rb', line 47

def route(path, method = nil, &block)
  route_builder = RouteBuilder.new(path, method, parent_path: @full_prefix, &block)
  @mod_builders << route_builder
  route_builder
end

#shared(*mods, &block) ⇒ Object



41
42
43
44
# File 'lib/meta/route_dsl/application_builder.rb', line 41

def shared(*mods, &block)
  @shared_mods += mods
  @shared_mods << Module.new(&block) if block_given?
end