Class: Belt::Application::RouteBuilder
- Inherits:
-
Object
- Object
- Belt::Application::RouteBuilder
- Defined in:
- lib/belt/route_dsl.rb
Instance Method Summary collapse
-
#initialize(gateway) ⇒ RouteBuilder
constructor
A new instance of RouteBuilder.
- #lambda(name) ⇒ Object
- #mount(mountable, options = {}) ⇒ Object
- #mount_full_path(path, prefix) ⇒ Object
- #mount_route(route_def, prefix, extra_tables, auth_override) ⇒ Object
- #mount_route_options(route_def, path, prefix, extra_tables, auth_override) ⇒ Object
- #resource(name, options = {}) ⇒ Object
- #resources(name, options = {}) ⇒ Object
- #scope(options = {}) ⇒ Object
Constructor Details
#initialize(gateway) ⇒ RouteBuilder
Returns a new instance of RouteBuilder.
303 304 305 306 307 308 309 310 |
# File 'lib/belt/route_dsl.rb', line 303 def initialize(gateway) @gateway = gateway @scope_prefix = '' @scope_module = nil @scope_auth = nil @scope_tables = [] @scope_controller = nil end |
Instance Method Details
#lambda(name) ⇒ Object
359 360 361 |
# File 'lib/belt/route_dsl.rb', line 359 def lambda(name, &) name end |
#mount(mountable, options = {}) ⇒ Object
363 364 365 366 367 368 369 370 371 372 |
# File 'lib/belt/route_dsl.rb', line 363 def mount(mountable, = {}) prefix = [:at]&.to_s&.gsub(%r{^/|/$}, '') || '' extra_tables = Array([:tables] || []) auth_override = [:auth] route_definitions = mountable.respond_to?(:routes) ? mountable.routes : [] route_definitions.each do |route_def| mount_route(route_def, prefix, extra_tables, auth_override) end end |
#mount_full_path(path, prefix) ⇒ Object
383 384 385 386 387 |
# File 'lib/belt/route_dsl.rb', line 383 def mount_full_path(path, prefix) full_path = prefix.empty? ? path : "/#{prefix}#{path}" full_path = full_path.chomp('/') unless full_path == '/' build_path(full_path) end |
#mount_route(route_def, prefix, extra_tables, auth_override) ⇒ Object
374 375 376 377 378 379 380 381 |
# File 'lib/belt/route_dsl.rb', line 374 def mount_route(route_def, prefix, extra_tables, auth_override) method = route_def[:method].to_sym path = route_def[:path].to_s.gsub(/:([a-zA-Z_]\w*)/) { "{#{::Regexp.last_match(1)}}" } full_path = mount_full_path(path, prefix) = (route_def, path, prefix, extra_tables, auth_override) @gateway.send(method, full_path, ) end |
#mount_route_options(route_def, path, prefix, extra_tables, auth_override) ⇒ Object
389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/belt/route_dsl.rb', line 389 def (route_def, path, prefix, extra_tables, auth_override) = (route_def[:options] || {}).dup [:tables] = (extra_tables + Array([:tables] || [])).uniq [:auth] = auth_override if auth_override [:auth] ||= @scope_auth if @scope_auth [:tables] = (@scope_tables + [:tables]).uniq if @scope_tables.any? [:controller] ||= prefix.gsub('-', '_') unless prefix.empty? stripped = path.gsub(%r{^/|/$}, '') [:action] ||= stripped.empty? ? 'index' : stripped.gsub('-', '_') end |
#resource(name, options = {}) ⇒ Object
354 355 356 357 |
# File 'lib/belt/route_dsl.rb', line 354 def resource(name, = {}) = () @gateway.resource(name, ) end |
#resources(name, options = {}) ⇒ Object
349 350 351 352 |
# File 'lib/belt/route_dsl.rb', line 349 def resources(name, = {}, &) = () @gateway.resources(name, , &) end |
#scope(options = {}) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/belt/route_dsl.rb', line 312 def scope( = {}, &) previous_prefix = @scope_prefix previous_module = @scope_module previous_auth = @scope_auth previous_tables = @scope_tables previous_controller = @scope_controller @scope_prefix = [:path] || @scope_prefix @scope_module = [:module] || @scope_module @scope_auth = [:auth] || @scope_auth @scope_tables = (@scope_tables + Array([:tables] || [])).uniq @scope_controller = [:controller] || @scope_controller instance_eval(&) if block_given? @scope_prefix = previous_prefix @scope_module = previous_module @scope_auth = previous_auth @scope_tables = previous_tables @scope_controller = previous_controller end |