Class: Flame::Router::RouteRefine

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

Overview

Helper class for controller routing refine

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router, ctrl, path, block) ⇒ RouteRefine

Returns a new instance of RouteRefine.



76
77
78
79
80
81
82
# File 'lib/flame/router.rb', line 76

def initialize(router, ctrl, path, block)
	@router = router
	@ctrl = ctrl
	@path = path || @ctrl.default_path
	@routes = []
	execute(&block)
end

Instance Attribute Details

#ctrlObject (readonly)

Returns the value of attribute ctrl.



59
60
61
# File 'lib/flame/router.rb', line 59

def ctrl
  @ctrl
end

#rest_routesObject

Defaults REST routes (methods, pathes, controllers actions)



66
67
68
# File 'lib/flame/router.rb', line 66

def rest_routes
  @rest_routes
end

#routesObject (readonly)

Returns the value of attribute routes.



59
60
61
# File 'lib/flame/router.rb', line 59

def routes
  @routes
end

Class Method Details

.http_methodsObject



61
62
63
# File 'lib/flame/router.rb', line 61

def self.http_methods
	[:GET, :POST, :PUT, :DELETE]
end

Instance Method Details

#defaultsObject

Assign remaining methods of the controller

to defaults pathes and HTTP methods


116
117
118
119
120
121
122
# File 'lib/flame/router.rb', line 116

def defaults
	rest
	@ctrl.public_instance_methods(false).each do |action|
		next if find_route_index(action: action)
		send(:GET.downcase, action)
	end
end

#mount(ctrl, path = nil) { ... } ⇒ Object

Mount controller inside other (parent) controller

Parameters:

  • ctrl (Flame::Controller)

    class of mounting controller

  • path (String, nil) (defaults to: nil)

    root path for mounting controller

Yields:

  • Block of code for routes refine



138
139
140
141
# File 'lib/flame/router.rb', line 138

def mount(ctrl, path = nil, &block)
	path = Route.path_merge(@path, path || ctrl.default_path)
	@router.add_controller(ctrl, path, block)
end

#restObject

Assign methods of the controller to REST architecture



125
126
127
128
129
130
131
132
# File 'lib/flame/router.rb', line 125

def rest
	rest_routes.each do |rest_route|
		action = rest_route[:action]
		next unless @ctrl.public_instance_methods.include?(action) &&
		   find_route_index(action: action).nil?
		send(*rest_route.values.map(&:downcase), prefix: true)
	end
end