Class: Gambiarra::Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



5
6
7
8
9
10
11
12
13
14
15
# 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

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



3
4
5
# File 'lib/gambiarra/router.rb', line 3

def history
  @history
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/gambiarra/router.rb', line 3

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/gambiarra/router.rb', line 3

def path
  @path
end

#routesObject (readonly)

Returns the value of attribute routes.



3
4
5
# File 'lib/gambiarra/router.rb', line 3

def routes
  @routes
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/gambiarra/router.rb', line 3

def url
  @url
end

Instance Method Details

#get(**params) ⇒ Object



17
18
19
20
21
22
23
24
# 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_pathObject



26
27
28
# File 'lib/gambiarra/router.rb', line 26

def previous_path
  history.previous
end