Class: Meta::Application

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Execution::MakeToRackMiddleware
Includes:
Execution::MakeToRackMiddleware
Defined in:
lib/meta/application.rb,
lib/meta/application/application.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Execution::MakeToRackMiddleware

call

Constructor Details

#initialize(prefix: '', mods: [], shared_mods: [], error_guards: []) ⇒ Application

Returns a new instance of Application.



12
13
14
15
16
17
# File 'lib/meta/application/application.rb', line 12

def initialize(prefix: '', mods: [], shared_mods: [], error_guards: [])
  @prefix = Utils::Path.normalize_path(prefix)
  @mods = mods
  @shared_mods = shared_mods
  @error_guards = error_guards
end

Class Attribute Details

.builderObject (readonly)

Returns the value of attribute builder.



18
19
20
# File 'lib/meta/application.rb', line 18

def builder
  @builder
end

Instance Attribute Details

#error_guardsObject (readonly)

Returns the value of attribute error_guards.



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

def error_guards
  @error_guards
end

#modsObject (readonly)

Returns the value of attribute mods.



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

def mods
  @mods
end

#prefixObject (readonly)

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

Class Method Details

.appObject



33
34
35
# File 'lib/meta/application.rb', line 33

def app
  @app || @app = builder.build
end

.build(**args) ⇒ Object



37
38
39
# File 'lib/meta/application.rb', line 37

def build(**args)
  @app = builder.build(**args)
end

.inherited(mod) ⇒ Object



20
21
22
23
24
# File 'lib/meta/application.rb', line 20

def inherited(mod)
  super

  mod.instance_variable_set(:@builder, RouteDSL::ApplicationBuilder.new)
end

Instance Method Details

#applicationsObject



45
46
47
# File 'lib/meta/application/application.rb', line 45

def applications
  mods.filter { |r| r.is_a?(Application) }
end

#execute(execution, remaining_path = '') ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/meta/application/application.rb', line 19

def execute(execution, remaining_path = '')
  remaining_path_for_children = path_matching.merge_path_params(remaining_path, execution.request)

  @shared_mods.each { |mod| execution.singleton_class.include(mod) }

  mod = find_child_mod(execution, remaining_path_for_children)
  if mod
    mod.execute(execution, remaining_path_for_children)
  else
    request = execution.request
    raise Errors::NoMatchingRoute, "未能发现匹配的路由:#{request.request_method} #{request.path}"
  end
rescue StandardError => e
  guard = error_guards.find { |g| e.is_a?(g[:error_class]) }
  raise unless guard

  execution.instance_exec(e, &guard[:caller])
end

#match?(execution, remaining_path) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/meta/application/application.rb', line 38

def match?(execution, remaining_path)
  return false unless path_matching.match?(remaining_path)

  remaining_path_for_children = path_matching.capture_remaining_part(remaining_path)
  find_child_mod(execution, remaining_path_for_children) != nil
end

#routesObject



49
50
51
# File 'lib/meta/application/application.rb', line 49

def routes
  mods.filter { |r| r.is_a?(Route) }
end

#to_swagger_doc(options = {}) ⇒ Object



53
54
55
# File 'lib/meta/application/application.rb', line 53

def to_swagger_doc(options = {})
  SwaggerDocUtil.generate(self, **options)
end