Class: Flame::Router::RouteRefine

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

Overview

Helper module for routing refine

Constant Summary collapse

HOOK_TYPES =
[:before, :after, :error].freeze

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.



82
83
84
85
86
87
88
89
# File 'lib/flame/router.rb', line 82

def initialize(router, ctrl, path, block)
	@router = router
	@ctrl = ctrl
	@path = path || @ctrl.default_path
	@routes = []
	@hooks = HOOK_TYPES.each_with_object({}) { |type, hash| hash[type] = {} }
	execute(&block)
end

Instance Attribute Details

#ctrlObject (readonly)

Returns the value of attribute ctrl.



64
65
66
# File 'lib/flame/router.rb', line 64

def ctrl
  @ctrl
end

#hooksObject (readonly)

Returns the value of attribute hooks.



64
65
66
# File 'lib/flame/router.rb', line 64

def hooks
  @hooks
end

#rest_routesObject

Returns the value of attribute rest_routes.



63
64
65
# File 'lib/flame/router.rb', line 63

def rest_routes
  @rest_routes
end

#routesObject (readonly)

Returns the value of attribute routes.



64
65
66
# File 'lib/flame/router.rb', line 64

def routes
  @routes
end

Class Method Details

.http_methodsObject



68
69
70
# File 'lib/flame/router.rb', line 68

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

Instance Method Details

#defaultsObject



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

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



128
129
130
131
132
133
134
# File 'lib/flame/router.rb', line 128

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

#restObject



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

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