Class: Coltrane::UI::Router
- Inherits:
-
Object
- Object
- Coltrane::UI::Router
- Defined in:
- lib/coltrane/ui/router.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #build_url(path, params) ⇒ Object
- #draw_route(*args, **keyword_args, &block) ⇒ Object
- #get(**params) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #previous_path ⇒ Object
Constructor Details
#initialize ⇒ Router
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
#history ⇒ Object (readonly)
Returns the value of attribute history.
47 48 49 |
# File 'lib/coltrane/ui/router.rb', line 47 def history @history end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
47 48 49 |
# File 'lib/coltrane/ui/router.rb', line 47 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
47 48 49 |
# File 'lib/coltrane/ui/router.rb', line 47 def path @path end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
47 48 49 |
# File 'lib/coltrane/ui/router.rb', line 47 def routes @routes end |
#url ⇒ Object (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_path ⇒ Object
77 78 79 |
# File 'lib/coltrane/ui/router.rb', line 77 def previous_path history.previous end |