Class: Meta::RouteDSL::MetaBuilder
- Inherits:
-
Object
- Object
- Meta::RouteDSL::MetaBuilder
- Defined in:
- lib/meta/route_dsl/meta_builder.rb
Defined Under Namespace
Modules: Delegator
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(route_full_path:, route_method: :all, &block) ⇒ MetaBuilder
constructor
A new instance of MetaBuilder.
- #parameters(&block) ⇒ Object
-
#params(&block) ⇒ Object
params 宏是一个遗留的宏,它在一个宏定义块内同时定义 parameters 和 request_body.
- #request_body(options = {}, &block) ⇒ Object
- #scope(scope) ⇒ Object
- #status(code, *other_codes, **options, &block) ⇒ Object
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
#build ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/meta/route_dsl/meta_builder.rb', line 18 def build = @meta if @meta[:parameters].nil? && @route_full_path =~ /[:*].+/ [:parameters] = ParametersBuilder.new(route_full_path: @route_full_path, route_method: @method).build end 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( = {}, &block) @meta[:request_body] = JsonSchema::SchemaBuilderTool.build(, &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, **, &block) codes = [code, *other_codes] entity_schema = JsonSchema::SchemaBuilderTool.build(, &block) @meta[:responses] = @meta[:responses] || {} codes.each { |code| @meta[:responses][code] = entity_schema } end |