module Roda::RodaPlugins::RouteList::ClassMethods
Attributes
route_list[R]
Array of route metadata hashes.
Public Instance Methods
listed_route(name, args=nil)
click to toggle source
Return the path for the given named route. If args is not given, this returns the path directly. If args is a hash, any placeholder values in the path are replaced with the matching values in args. If args is an array, placeholder values are taken from the array in order.
# File lib/roda/plugins/route_list.rb 71 def listed_route(name, args=nil) 72 unless path = @route_list_names[name] 73 raise RodaError, "no route exists with the name: #{name.inspect}" 74 end 75 76 if args 77 if args.is_a?(Hash) 78 range = 1..-1 79 path = path.gsub(/:[^\/]+/) do |match| 80 unless value = args[match[range].to_sym] 81 raise RodaError, "no matching value exists in the hash for named route #{name}: #{match}" 82 end 83 value 84 end 85 else 86 values = args.dup 87 path = path.gsub(/:[^\/]+/) do |match| 88 if values.empty? 89 raise RodaError, "not enough placeholder values provided for named route #{name}: #{match}" 90 end 91 values.shift 92 end 93 94 unless values.empty? 95 raise RodaError, "too many placeholder values provided for named route #{name}" 96 end 97 end 98 end 99 100 path 101 end
Private Instance Methods
load_routes(file)
click to toggle source
Load the route metadata from the given json file.
# File lib/roda/plugins/route_list.rb 106 def load_routes(file) 107 @route_list_names = {} 108 109 routes = JSON.parse(File.read(file)) 110 @route_list = routes.map do |r| 111 path = r['path'].freeze 112 route = {:path=>path} 113 114 if methods = r['methods'] 115 route[:methods] = methods.map(&:to_sym) 116 end 117 118 if name = r['name'] 119 name = name.to_sym 120 route[:name] = name.to_sym 121 @route_list_names[name] = path 122 end 123 124 route.freeze 125 end.freeze 126 127 @route_list_names.freeze 128 129 nil 130 end