Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Router
- Route = Struct.new(:pattern, :block)
- class RouteSet
- def on(pattern, &block)
- Router.routes.push Route.new(pattern, block)
- end
- end
- @routes = []
- def self.routes
- @routes
- end
- def self.define(&block)
- route_set = RouteSet.new
- route_set.instance_eval(&block)
- end
- def self.run(path)
- params = {}
- matched_route = routes.find do |route|
- matched = path.match /\A#{route.pattern}\z/i
- params = matched.named_captures if matched
- !!matched
- end
- matched_route.block.call(params) if matched_route
- end
- end
- Router.define do
- on 'patterm' do
- # do something, return response
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement