Class: Tzispa::RouteSet

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

Constant Summary collapse

CONTROLLERS_BASE =
'Tzispa::Controller'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, root = nil) ⇒ RouteSet

Returns a new instance of RouteSet.



17
18
19
20
21
22
# File 'lib/tzispa/route_set.rb', line 17

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#map_pathObject (readonly)

Returns the value of attribute map_path.



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

def map_path
  @map_path
end

#routerObject (readonly)

Returns the value of attribute router.



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

def router
  @router
end

Instance Method Details

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



43
44
45
46
47
# File 'lib/tzispa/route_set.rb', line 43

def add(route_id, path, controller, methods: nil, matching: nil)
  add_route(route_id, path, to: build_controller(controller),
                            methods: methods,
                            matching: matching)
end

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



57
58
59
# File 'lib/tzispa/route_set.rb', line 57

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

#call(env) ⇒ Object



39
40
41
# File 'lib/tzispa/route_set.rb', line 39

def call(env)
  @router.call env
end

#drawObject



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

def draw
  yield if block_given?
end

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



53
54
55
# File 'lib/tzispa/route_set.rb', line 53

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

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



65
66
67
68
# File 'lib/tzispa/route_set.rb', line 65

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

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



35
36
37
# File 'lib/tzispa/route_set.rb', line 35

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

#routes_definitionsObject



31
32
33
# File 'lib/tzispa/route_set.rb', line 31

def routes_definitions
  @routes_definitions ||= "config/routes/#{app.name}.rb"
end

#setupObject



24
25
26
27
28
29
# File 'lib/tzispa/route_set.rb', line 24

def setup
  draw do
    contents = File.read(routes_definitions)
    instance_eval(contents, File.basename(routes_definitions), 0)
  end
end

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



61
62
63
# File 'lib/tzispa/route_set.rb', line 61

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