Class: Routing::Router

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

Constant Summary collapse

PAGE_CONTAINER =
'page'.freeze
SEGMENT_DIVIDER =
'/'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, web_svr_obj) ⇒ Router

Set up the web server.



23
24
25
26
27
28
29
# File 'lib/routing/router.rb', line 23

def initialize( engine, web_svr_obj )
  @engine = engine
  @log = @engine.log

  @web_svr_obj = web_svr_obj
  @show_routes = ShowRoutes.new( @engine )
end

Instance Attribute Details

#route_segmentsObject (readonly)

Returns the value of attribute route_segments.



13
14
15
# File 'lib/routing/router.rb', line 13

def route_segments
  @route_segments
end

Instance Method Details

#add_page_routesObject

Add all page routes to the web server pages (routes).



92
93
94
95
96
97
98
99
100
# File 'lib/routing/router.rb', line 92

def add_page_routes
  can = page_container
  return unless can

  @log.debug 'Adding page routes to web server…'
  @factory = @engine.factory

  add_pages can, @web_svr_obj.pages_container
end

#add_pages(can, parent) ⇒ Object

Add the pages to the web server pages. This is a recursive function that will add all pages in the folder and subfolders.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/routing/router.rb', line 107

def add_pages can, parent
  # for each file in the page container
  # create a page object and add it to the routes
  can.children.each do |obj|
    if obj.class == Gloo::Objs::Container
      child_can = parent.find_add_child( obj.name, 'container' )
      add_pages( obj, child_can )
    elsif obj.class == Objs::Page
      add_route_alias( parent, obj.name, obj.pn )
    end
  end
end

#add_route_alias(parent, name, pn) ⇒ Object

Add route alias to the page.



123
124
125
126
127
128
129
130
131
# File 'lib/routing/router.rb', line 123

def add_route_alias( parent, name, pn )
  name = name.gsub( '.', '_' )

  # First make sure the child doesn't already exist.
  child = parent.find_child( name )
  return if child

  @factory.create_alias( name, pn, parent )
end

#detect_segments(path) ⇒ Object

Create a list of path segments.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/routing/router.rb', line 201

def detect_segments path
  # For Assets, substitute the published name with fingerprint
  # for the simple asset name (if it is found).
  asset_info = WebSvr::AssetInfo.find_info_for( path )
  unless asset_info.blank?
    path = asset_info.pn
  end

  # Split the path into segments.
  @route_segments = path.split SEGMENT_DIVIDER

  # Remove the first segment if it is empty.
  @route_segments.shift if @route_segments.first.blank?

  @route_segments.each do |seg|
    if seg.to_i.to_s == seg
      @id = seg.to_i
      @log.info "found id for route: #{@id}"
      @route_segments.delete seg
      @route_segments << ResourceRouter.segment_for_method( @method )
    end
  end

  return @route_segments
end

#find_route_segment(objs) ⇒ Object

Find the route segment in the object container.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/routing/router.rb', line 149

def find_route_segment objs
  this_segment = next_segment

  if this_segment.blank? # && WebSvr::WebMethod.is_post?( @method )
    this_segment = Routing::ResourceRouter::INDEX 
  end

  objs.each do |o|
    o = Gloo::Objs::Alias.resolve_alias( @engine, o )

    if o.name == this_segment
      if o.class == Objs::Page
        @log.debug "found page for route: #{o.pn}"
        return o
      elsif o.class == Gloo::Objs::FileHandle
        @log.debug "found static file for route: #{o.pn}"
        return o
      else
        return nil unless o.child_count > 0

        return find_route_segment( o.children )
      end
    end
  end

  return nil 
end

#is_root_path?Boolean

Is this the root path?

Returns:

  • (Boolean)


194
195
196
# File 'lib/routing/router.rb', line 194

def is_root_path?
  return @route_segments.count == 0
end

#next_segmentObject

Get the next segment in the route.



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/routing/router.rb', line 180

def next_segment
  this_segment = @route_segments.shift
  return nil if this_segment.nil?

  # A URL might include a dot in a name, but we can't do that
  # because dot is a reserve path thing. So we replace it with
  # an underscore.
  this_segment = this_segment.gsub( '.', '_' )

  return this_segment
end

#page_containerObject

Get the root level page container.



84
85
86
87
# File 'lib/routing/router.rb', line 84

def page_container
  pn = Gloo::Core::Pn.new( @engine, PAGE_CONTAINER )
  return pn.resolve
end

#page_for_route(path, method) ⇒ Object

Find and return the page for the given route.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/routing/router.rb', line 39

def page_for_route( path, method )
  @log.info "routing to #{path} for method #{method}"
  @method = method
  route_params = nil
  @id = nil
  
  detect_segments path

  return @web_svr_obj.home_page if is_root_path?

  pages = @web_svr_obj.pages_container
  if pages
    # If the method is POST and the last segment is NOT 'create',
    # we'll add create to the route segments.
    if Routing::ResourceRouter.is_implicit_create?( method, @route_segments.last )
      @route_segments << Routing::ResourceRouter::POST_ROUTE
      page = find_route_segment( pages.children )
      return [ page, @id, route_params ] if page

      # We didn't find the page, so remove the last segment and try again 
      # posting to the resource.
      @route_segments.pop
    end

    page = find_route_segment( pages.children ) 
    
    # Are there any remaining segments to be added as route parameters?
    if @route_segments.count > 0
      route_params = @route_segments
    end

    return [ page, @id, route_params ] if page
  end

  return nil
end

#show_routesObject

Show the routes in the running app. This uses the ShowRoutes helper class.



142
143
144
# File 'lib/routing/router.rb', line 142

def show_routes
  @show_routes.show page_container
end