Class: Meta::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/meta/application/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: '', method: :all, meta: {}, action: nil) ⇒ Route

path 是局部 path,不包含由父级定义的前缀



14
15
16
17
18
19
# File 'lib/meta/application/route.rb', line 14

def initialize(path: '', method: :all, meta: {}, action: nil)
  @path = Utils::Path.normalize_path(path)
  @method = method
  @meta = Metadata.new(meta)
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



11
12
13
# File 'lib/meta/application/route.rb', line 11

def action
  @action
end

#metaObject (readonly)

Returns the value of attribute meta.



11
12
13
# File 'lib/meta/application/route.rb', line 11

def meta
  @meta
end

#methodObject (readonly)

Returns the value of attribute method.



11
12
13
# File 'lib/meta/application/route.rb', line 11

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/meta/application/route.rb', line 11

def path
  @path
end

Instance Method Details

#execute(execution, remaining_path) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/meta/application/route.rb', line 21

def execute(execution, remaining_path)
  path_matching.merge_path_params(remaining_path, execution.request)

  execution.route_meta = @meta # 解析参数的准备
  action.execute(execution) if action
  @meta.set_response(execution) if @meta.responses
rescue Execution::Abort
  execution.response.status = 200 if execution.response.status == 0
end

#generate_operation_doc(schemas) ⇒ Object



41
42
43
44
# File 'lib/meta/application/route.rb', line 41

def generate_operation_doc(schemas)
  # 不再自动添加 $post 等 scope
  meta.generate_operation_doc(schemas, scope: [])
end

#match?(execution, remaining_path) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/meta/application/route.rb', line 31

def match?(execution, remaining_path)
  request = execution.request
  remaining_path = '' if remaining_path == '/'
  method = request.request_method

  return false unless path_matching.match?(remaining_path)
  return false unless @method == :all || @method.to_s.upcase == method
  return true
end