Class: Flame::Router::RouteRefine

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

Overview

Helper module for 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.



73
74
75
76
77
78
79
80
# File 'lib/flame/router.rb', line 73

def initialize(router, ctrl, path, block)
	@router = router
	@ctrl = ctrl
	@path = path || @ctrl.default_path
	@routes = []
	@befores, @afters = Array.new(2) { {} }
	execute(&block)
end

Instance Attribute Details

#aftersObject (readonly)

Returns the value of attribute afters.



57
58
59
# File 'lib/flame/router.rb', line 57

def afters
  @afters
end

#beforesObject (readonly)

Returns the value of attribute befores.



57
58
59
# File 'lib/flame/router.rb', line 57

def befores
  @befores
end

#ctrlObject (readonly)

Returns the value of attribute ctrl.



57
58
59
# File 'lib/flame/router.rb', line 57

def ctrl
  @ctrl
end

#rest_routesObject

Returns the value of attribute rest_routes.



56
57
58
# File 'lib/flame/router.rb', line 56

def rest_routes
  @rest_routes
end

#routesObject (readonly)

Returns the value of attribute routes.



57
58
59
# File 'lib/flame/router.rb', line 57

def routes
  @routes
end

Class Method Details

.http_methodsObject



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

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

Instance Method Details

#after(actions = :*, action = nil, &block) ⇒ Object



98
99
100
101
# File 'lib/flame/router.rb', line 98

def after(actions = :*, action = nil, &block)
	actions = [actions] unless actions.is_a?(Array)
	actions.each { |a| (@afters[a] ||= []).push(action || block) }
end

#before(actions = :*, action = nil, &block) ⇒ Object



93
94
95
96
# File 'lib/flame/router.rb', line 93

def before(actions = :*, action = nil, &block)
	actions = [actions] unless actions.is_a?(Array)
	actions.each { |a| (@befores[a] ||= []).push(action || block) }
end

#defaultsObject



103
104
105
106
107
108
109
# File 'lib/flame/router.rb', line 103

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

#mount(ctrl, path = nil, &block) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/flame/router.rb', line 121

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

#restObject



111
112
113
114
115
116
117
118
119
# File 'lib/flame/router.rb', line 111

def rest
	rest_routes.each do |rest_route|
		action = rest_route[:action]
		if @ctrl.public_instance_methods.include?(action) &&
		   find_route_index(action: action).nil?
			add_route(*rest_route.values, true)
		end
	end
end