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(map_path = nil) ⇒ Routes

Returns a new instance of Routes.



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

def initialize(map_path=nil)
  @router = HttpRouter.new
  @map_path = map_path unless map_path=='/'
end

Instance Attribute Details

#map_pathObject (readonly)

Returns the value of attribute map_path.



13
14
15
# File 'lib/tzispa/routes.rb', line 13

def map_path
  @map_path
end

#routerObject (readonly)

Returns the value of attribute router.



13
14
15
# File 'lib/tzispa/routes.rb', line 13

def router
  @router
end

Instance Method Details

#add(route_id, path, controller, methods) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tzispa/routes.rb', line 24

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

#api(path, methods, controller = nil) ⇒ Object



45
46
47
# File 'lib/tzispa/routes.rb', line 45

def api(path, methods, controller=nil)
  add :api, path, controller || 'api:dispatch!', methods
end

#index(path, methods, controller = nil) ⇒ Object



41
42
43
# File 'lib/tzispa/routes.rb', line 41

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

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



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

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

#site(path, methods, controller = nil) ⇒ Object



49
50
51
# File 'lib/tzispa/routes.rb', line 49

def site(path, methods, controller=nil)
  add :site, path, controller || 'layout:render!', methods
end