Skip to content

Instantly share code, notes, and snippets.

@pangloss pangloss/router.cljs
Created Sep 29, 2013

Embed
What would you like to do?
The expected result:
>> show(search.simple_router.route("/model/tweet/rel/responses"))
{:type :model, :model [tweet], :child {:type :rel, :rel responses}}
The actual result:
>> show(search.logic_router.routes(1, "/model/tweet/rel/responses"))
({:type :model, :model <lvar:value_238>, :child <lvar:child_241>})
(ns search.logic-router
(:require [clojure.string :as s])
(:require-macros [cljs.core.logic.macros :refer [== run fresh conde]])
(:use [cljs.core.logic :only [membero conso nilo emptyo partial-map]]))
(defn extract-parts [path]
(let [[path query] (s/split path #"\?")
path (remove #{""} (s/split path #"/"))
[query anchor] (when query (s/split query #"#"))
query (if query
(->> (s/split query #"&")
(map #(s/split % #"="))
vec)
[])]
[path query anchor]))
(declare *id -segment -filter)
(defn next-segment [path query route-map]
(conde
[(emptyo path) (nilo route-map) (== query [])]
[(*id path query route-map)]
[(-segment :model :model path query route-map)]
[(-segment :rel :rel path query route-map)]
[(-segment :to :to path query route-map)]
[(-filter path query route-map)]))
(defn *id [path query route-map]
(fresh [value r next-path child]
(== route-map {:type :id :id value :child child})
(conso "id" r path)
(conso value next-path r)
(conde
[(emptyo next-path) (nilo child) (== query [])]
[(-segment :model :model path query route-map)]
[(-segment :rel :rel path query route-map)]
[(-segment :to :to path query route-map)]
[(-filter next-path query child)])))
(defn -segment [segment value-key path query route-map]
(fresh [value r next-path child]
(== route-map {:type segment value-key value :child child})
(conso (name segment) r path)
(conso value next-path r)
(next-segment next-path query child)))
(defn -filter [path query route-map]
(fresh [name value r next-path child]
(== route-map {:type :filter :filter name :value value :child child})
(conso "filters" r path)
(conso name next-path r)
(conde
[(membero [name value] query)]
[nilo value])
(next-segment next-path query child)))
(defn r* [path query route-map]
(-segment :is :parts path query route-map))
(defn routes [n path]
(let [[path query anchor] (extract-parts path)]
(run n [q] (r* path query q))))
(defn paths [n route-map]
(run n [q]
(fresh [path query]
(== q [path query])
(r* path query route-map))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.