Class: Coltrane::UI::Router

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

Defined Under Namespace

Classes: History, Route

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



49
50
51
52
53
54
55
56
57
# File 'lib/coltrane/ui/router.rb', line 49

def initialize
  @routes  = []
  @history = History.new

  draw_route '', to: Coltrane::UI::Views::Index
  Views.constants.each do |view|
    draw_route view.to_s.underscore.humanize.downcase, to: "Coltrane::UI::Views::#{view}".constantize
  end
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



47
48
49
# File 'lib/coltrane/ui/router.rb', line 47

def history
  @history
end

#paramsObject (readonly)

Returns the value of attribute params.



47
48
49
# File 'lib/coltrane/ui/router.rb', line 47

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



47
48
49
# File 'lib/coltrane/ui/router.rb', line 47

def path
  @path
end

#routesObject (readonly)

Returns the value of attribute routes.



47
48
49
# File 'lib/coltrane/ui/router.rb', line 47

def routes
  @routes
end

#urlObject (readonly)

Returns the value of attribute url.



47
48
49
# File 'lib/coltrane/ui/router.rb', line 47

def url
  @url
end

Instance Method Details

#build_url(path, params) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/coltrane/ui/router.rb', line 68

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) ⇒ Object



81
82
83
# File 'lib/coltrane/ui/router.rb', line 81

def draw_route(*args, **keyword_args, &block)
  @routes << Route.new(*args, **keyword_args, &block)
end

#get(**params) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/coltrane/ui/router.rb', line 59

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.render(**params)
end

#previous_pathObject



77
78
79
# File 'lib/coltrane/ui/router.rb', line 77

def previous_path
  history.previous
end