Class: Brut::FrontEnd::Routing

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/front_end/routing.rb

Overview

Holds the registered routes for this app.

Defined Under Namespace

Classes: FormHandlerRoute, FormRoute, MissingForm, MissingHandler, MissingPage, MissingPath, PageRoute, Route

Instance Method Summary collapse

Constructor Details

#initializeRouting

Returns a new instance of Routing.



7
8
9
# File 'lib/brut/front_end/routing.rb', line 7

def initialize
  @routes = Set.new
end

Instance Method Details

#for(path:, method:) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/brut/front_end/routing.rb', line 11

def for(path:,method:)
  http_method = Brut::FrontEnd::HttpMethod.new(method)
  @routes.detect { |route|
    route.path_template == path &&
      route.http_method == http_method
  }
end

#inspectObject



130
131
132
133
134
# File 'lib/brut/front_end/routing.rb', line 130

def inspect
  @routes.map { |route|
    "#{route.http_method}:#{route.path_template} - #{route.handler_class.name}"
  }.join("\n")
end

#path(handler_class, with_method: :any, **rest) ⇒ Object



120
121
122
123
# File 'lib/brut/front_end/routing.rb', line 120

def path(handler_class, with_method: :any, **rest)
  route = self.route_for(handler_class, with_method:)
  route.path(**rest)
end

#register_form(path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/brut/front_end/routing.rb', line 55

def register_form(path)
  route = begin
            FormRoute.new(path)
          rescue Brut::Framework::Errors::NoClassForPath => ex
            if Brut.container.project_env.development?
              MissingForm.new(path,ex)
            else
              raise ex
            end
          end
  @routes << route
  add_routing_method(route)
  route
end

#register_handler_only(path) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/brut/front_end/routing.rb', line 70

def register_handler_only(path)
  route = begin
            FormHandlerRoute.new(path)
          rescue Brut::Framework::Errors::NoClassForPath => ex
            if Brut.container.project_env.development?
              MissingHandler.new(path,ex)
            else
              raise ex
            end
          end
  @routes << route
  add_routing_method(route)
  route
end

#register_page(path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/brut/front_end/routing.rb', line 40

def register_page(path)
  route = begin
            PageRoute.new(path)
          rescue Brut::Framework::Errors::NoClassForPath => ex
            if Brut.container.project_env.development?
              MissingPage.new(path,ex)
            else
              raise ex
            end
          end
  @routes << route
  add_routing_method(route)
  route
end

#register_path(path, method:) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/brut/front_end/routing.rb', line 85

def register_path(path, method:)
  route = begin
            Route.new(method, path)
          rescue Brut::Framework::Errors::NoClassForPath => ex
            if Brut.container.project_env.development?
              MissingPath.new(method,path,ex)
            else
              raise ex
            end
          end
  @routes << route
  add_routing_method(route)
  route
end

#reloadObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/brut/front_end/routing.rb', line 19

def reload
  new_routes = @routes.map { |route|
    if route.class == Route
      route.class.new(route.http_method,route.path_template)
    elsif route.class == MissingPage || route.class == MissingHandler || route.class == MissingForm
      route.class.new(route.path_template,route.exception)
    elsif route.class == MissingPath
      route.class.new(route.method,route.path_template,route.exception)
    else
      route.class.new(route.path_template)
    end
  }
  @routes = Set.new(new_routes)
  @routes.each do |route|
    handler_class = route.handler_class
    if handler_class.name !~ /^Brut::[A-Z]/
      add_routing_method(route)
    end
  end
end

#route(handler_class) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/brut/front_end/routing.rb', line 100

def route(handler_class)
  route = @routes.detect { |route|
    handler_class_match = route.handler_class.name == handler_class.name
    form_class_match = if route.respond_to?(:form_class)
                         route.form_class.name == handler_class.name
                       else
                         false
                       end
    handler_class_match || form_class_match
  }
  if !route
    if handler_class.ancestors.include?(Brut::FrontEnd::Form)
      raise ArgumentError,"There is no configured route for the form #{handler_class} and/or the handler class for this form doesn't exist"
    else
      raise ArgumentError,"There is no configured route for #{handler_class}"
    end
  end
  route
end

#url(handler_class, with_method: :any, **rest) ⇒ Object



125
126
127
128
# File 'lib/brut/front_end/routing.rb', line 125

def url(handler_class, with_method: :any, **rest)
  route = self.route_for(handler_class, with_method:)
  route.url(**rest)
end