Class: Tzispa::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/tzispa/routes.rb

Constant Summary collapse

CONTROLLERS_BASE =
'Tzispa::Controller'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, root = nil) ⇒ Routes

Returns a new instance of Routes.



19
20
21
22
23
24
# File 'lib/tzispa/routes.rb', line 19

def initialize(app, root=nil)
  @router = HttpRouter.new
  @app = app
  @router.default Tzispa::Controller::HttpError.new(app, :error_404)
  @map_path = root unless root=='/'
end

Instance Attribute Details

#map_pathObject (readonly)

Returns the value of attribute map_path.



17
18
19
# File 'lib/tzispa/routes.rb', line 17

def map_path
  @map_path
end

#routerObject (readonly)

Returns the value of attribute router.



17
18
19
# File 'lib/tzispa/routes.rb', line 17

def router
  @router
end

Instance Method Details

#path(path_id, params = {}) ⇒ Object



26
27
28
# File 'lib/tzispa/routes.rb', line 26

def path(path_id, params={})
  "#{@map_path}#{@router.path path_id, params}"
end

#route_rig_api(path, controller: nil, methods: nil) ⇒ Object



55
56
57
# File 'lib/tzispa/routes.rb', line 55

def route_rig_api(path, controller: nil, methods:nil)
  routing :api, path, controller || 'api:dispatch!', methods: methods
end

#route_rig_index(path, controller: nil, methods: nil) ⇒ Object



51
52
53
# File 'lib/tzispa/routes.rb', line 51

def route_rig_index(path, controller: nil, methods: nil)
  routing :index, path, controller || 'layout:render!', methods: methods
end

#route_rig_layout(layout, path, controller: nil, methods: nil) ⇒ Object



63
64
65
# File 'lib/tzispa/routes.rb', line 63

def route_rig_layout(layout, path, controller: nil, methods: nil)
  routing layout, path, controller || 'layout:render!', methods: methods, matching: {layout: layout.to_s}
end

#route_rig_signed_api(path, controller: nil, methods: nil) ⇒ Object



59
60
61
# File 'lib/tzispa/routes.rb', line 59

def route_rig_signed_api(path, controller: nil, methods: nil)
  routing :sapi, path, controller || 'api:dispatch!', methods: methods
end

#routing(route_id, path, controller, methods: nil, matching: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tzispa/routes.rb', line 30

def routing(route_id, path, controller, methods: nil, matching: nil)
  spec_control, callmethod = controller.to_s.split(':')
  mpath = spec_control.split('#')
  req_controller = mpath.pop
  controller = req_controller.camelize
  if mpath.count > 1
    controller_module = mpath.collect!{ |w| w.capitalize }.join('::')
    require_relative "./controller/#{req_controller}"
  else
    controller_module = CONTROLLERS_BASE
    require "tzispa/controller/#{req_controller}"
  end
  @router.add(path).tap { |rule|
    rule.to "#{controller_module}::#{controller}".constantize.new(@app, callmethod)
    rule.name = route_id
    rule.add_request_method(methods) if methods
    rule.add_match_with(matching) if matching
  }
end