Class: MonkeysPaw::Router
- Inherits:
-
Object
- Object
- MonkeysPaw::Router
- Defined in:
- lib/monkeyspaw/router.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #get_file(path) ⇒ Object
- #get_route_for_file(file) ⇒ Object
-
#initialize(app) ⇒ Router
constructor
A new instance of Router.
- #next_page(current_path) ⇒ Object
- #prev_page(current_path) ⇒ Object
- #scan_pages ⇒ Object
Constructor Details
#initialize(app) ⇒ Router
Returns a new instance of Router.
5 6 7 8 9 |
# File 'lib/monkeyspaw/router.rb', line 5 def initialize(app) @app = app @routes = {} scan_pages end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
3 4 5 |
# File 'lib/monkeyspaw/router.rb', line 3 def app @app end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
3 4 5 |
# File 'lib/monkeyspaw/router.rb', line 3 def routes @routes end |
Instance Method Details
#get_file(path) ⇒ Object
31 32 33 |
# File 'lib/monkeyspaw/router.rb', line 31 def get_file(path) @routes[path] end |
#get_route_for_file(file) ⇒ Object
49 50 51 |
# File 'lib/monkeyspaw/router.rb', line 49 def get_route_for_file(file) @routes.key(file) end |
#next_page(current_path) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/monkeyspaw/router.rb', line 35 def next_page(current_path) keys = @routes.keys current_index = keys.find_index(current_path) return nil unless current_index keys[current_index + 1] end |
#prev_page(current_path) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/monkeyspaw/router.rb', line 42 def prev_page(current_path) keys = @routes.keys current_index = keys.find_index(current_path) return nil unless current_index || current_index == 0 keys[current_index - 1] end |
#scan_pages ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/monkeyspaw/router.rb', line 11 def scan_pages @routes = {} pages_dir = app.pages_dir return unless pages_dir.exist? page_files = Dir.glob(pages_dir.join('**', '*.md')) page_files.each do |file| rel_path = Pathname.new(file).relative_path_from(pages_dir) route = '/' + rel_path.to_s.sub(/\.md$/, '') route = route.sub(/\/index$/, '/') @routes[route] = file end @routes = @routes.sort_by { |route, file| file }.to_h end |