class Gambiarra::Router

Attributes

history[R]
params[R]
path[R]
routes[R]
url[R]

Public Class Methods

new() click to toggle source
# File lib/gambiarra/router.rb, line 5
def initialize
  @routes  = []
  @history = History.new

  base_view = View.descendants[:BaseView]
  draw_route '', to: base_view.descendants[:Index]
  base_view.descendants.each do |name, view|
    next if name == :Index
    draw_route name.to_s.underscore.humanize.downcase, to: view
  end
end

Public Instance Methods

get(**params) click to toggle source
# File lib/gambiarra/router.rb, line 17
def get(**params)
  path = params.delete(:path)
  @url  = build_url(path || history.current_route.path, **params)
  route = routes.detect { |route| route.path == path }
  return history.refresh(**params) unless route
  history.add(route)
  route.respond(**params)
end
previous_path() click to toggle source
# File lib/gambiarra/router.rb, line 26
def previous_path
  history.previous
end

Private Instance Methods

build_url(path, **params) click to toggle source
# File lib/gambiarra/router.rb, line 32
def build_url(path, **params)
  [
    path,
    (params || {}).map do |k,v|
      [k,v.gsub(' ', '-')].join(':')
    end
  ].compact.join(' ')
end
draw_route(*args, **keyword_args, &block) click to toggle source
# File lib/gambiarra/router.rb, line 41
def draw_route(*args, **keyword_args, &block)
  @routes << Route.new(*args, **keyword_args, &block)
end