On Saturday, May 12, 2012 12:44:45 AM UTC-5, Baishampayan Ghose wrote:
>
>
> There are many ways of doing this. One approach that I have seen a lot
> is something like this -
>
> ;; core.clj
>
> (def ^:dynamic *settings* {:default :stuff}) ;; the default settings can
> be nil
>
I was experimenting with the dynamic varr approach...
;; bulbs/neo4jserver/client.clj
(def ^:dynamic *config* default-config)
(defn set-config!
[config]
(alter-var-root #'*config* (fn [_] (merge default-config config))))
(defn neo4j-client
[& config]
(set-config! (first config)))
(neo4j-client {:root_uri "http://localhost:7474/data/db/"})
(println *config*)
...but Andrew Cooke pointed out that using a global var would preclude you
from being able to use multiple, independent graph "instances" in your
program, whereas you can in the Python version (
http://stackoverflow.com/questions/10540999/how-to-configure-a-clojure-library-at-runtime
).
And so I came up with this:
(defn graph
[& config]
(let [config (get-config (first config))]
(fn [func & args]
(apply func config args))))
(defn create-vertex
[config data]
(let [path (build-path vertex-path)
params (remove-null-values data)]
(rest/post config path params)))
(defn gremlin
[config script & params]
(rest/post config gremlin-path {:script script :params (first params)}))
And then you can call the different functions like this:
(def g (graph {:root_uri "http://localhost:7474/data/db/"}))
(g create-vertex {:name "James"})
(g gremlin "g.v(id)" {:id 178})
Now I haven't delved into macros yet, and I'm not too sure of the merits of
this approach compared to others so feedback welcome.
- James
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en