Class: Bridgetown::Routes::Manifest
- Inherits:
-
Object
- Object
- Bridgetown::Routes::Manifest
- Defined in:
- bridgetown-routes/lib/bridgetown-routes/manifest.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#manifest ⇒ Object
readonly
Returns the value of attribute manifest.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
-
#expand_source_paths_with_islands ⇒ Object
-
#file_slug_and_segments(routes_dir, file) ⇒ Object
-
#generate_localized_file_slugs(file_slug) ⇒ Object
-
#glob_routes(dir, pattern = "**/*") ⇒ Object
-
#initialize(site, cache_routes: Bridgetown.env.production?) ⇒ Manifest
constructor
A new instance of Manifest.
-
#locale_for(slug) ⇒ Object
-
#routable_extensions ⇒ Object
-
#routes ⇒ Object
-
#sort_routes!(routes) ⇒ Object
Constructor Details
#initialize(site, cache_routes: Bridgetown.env.production?) ⇒ Manifest
Returns a new instance of Manifest.
8 9 10 11 12 13 14 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 8 def initialize(site, cache_routes: Bridgetown.env.production?) @site = site @manifest = [] @config = site.config.routes @cache_routes = cache_routes @islands_dir = File.(site.config.islands_dir, site.config.source) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 6 def config @config end |
#manifest ⇒ Object (readonly)
Returns the value of attribute manifest.
6 7 8 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 6 def manifest @manifest end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
6 7 8 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 6 def site @site end |
Instance Method Details
#expand_source_paths_with_islands ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 39 def # clear out any past islands folders config.source_paths.reject! { _1.start_with?(@islands_dir) } Dir.glob("#{@islands_dir}/**/routes").each do |route_folder| config.source_paths << route_folder end config.source_paths.map { File. _1, site.config.source } end |
#file_slug_and_segments(routes_dir, file) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 58 def file_slug_and_segments(routes_dir, file) # @type [String] file_slug = file.delete_prefix("#{routes_dir}/").then do |f| if routes_dir.start_with?(@islands_dir) # convert _islands/foldername/routes/someroute.rb to foldername/someroute.rb f = routes_dir.delete_prefix("#{@islands_dir}/").sub(%r!/routes$!, "/") + f end [File.dirname(f), File.basename(f, ".*")].join("/").delete_prefix("./") end.delete_suffix("/index") segment_keys = [] file_slug.gsub!(%r{\[([^/]+)\]}) do |_segment| segment_keys << Regexp.last_match(1) ":#{Regexp.last_match(1)}" end [file_slug, segment_keys] end |
#generate_localized_file_slugs(file_slug) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 76 def generate_localized_file_slugs(file_slug) site.config.available_locales.map do |locale| if locale == site.config.default_locale && !site.config.prefix_default_locale file_slug else "#{locale}/#{file_slug}" end end end |
#glob_routes(dir, pattern = "**/*") ⇒ Object
50 51 52 53 54 55 56 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 50 def glob_routes(dir, pattern = "**/*") files = Dir.glob("#{dir}/#{pattern}.{#{routable_extensions}}") files.reject! do |file| File.basename(file, ".*").then { _1.start_with?("_", ".") || _1.end_with?(".test") } end files end |
#locale_for(slug) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 103 def locale_for(slug) possible_locale_segment = slug.split("/").first.to_sym if site.config.available_locales.include? possible_locale_segment possible_locale_segment else site.config.default_locale end end |
#routable_extensions ⇒ Object
16 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 16 def routable_extensions = config.extensions.join(",") |
#routes ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 18 def routes return @manifest if !@manifest.empty? && @cache_routes @manifest = [] # Loop through all the directories (`src/_routes`, etc) looking for route files, then # sort them and add them to the manifest: .each do |routes_dir| @manifest += glob_routes(routes_dir).map do |file| file_slug, segment_keys = file_slug_and_segments(routes_dir, file) # generate localized file slugs localized_file_slugs = generate_localized_file_slugs(file_slug) [file, localized_file_slugs, segment_keys] end.then { sort_routes! _1 } end @manifest end |
#sort_routes!(routes) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'bridgetown-routes/lib/bridgetown-routes/manifest.rb', line 86 def sort_routes!(routes) routes.sort! do |route_a, route_b| # @type [String] slug_a = route_a[1][0] # @type [String] slug_b = route_b[1][0] # @type [Integer] weight1 = slug_a.count("/") <=> slug_b.count("/") if weight1.zero? slug_b.count("/:") <=> slug_a.count("/:") else weight1 end end.reverse! end |