Permalink
Browse files

seperate to 3 single commands: setup, model and view

  • Loading branch information...
1 parent 8ab404d commit fc9c9e816348f355461cac55f606a72b3ed1283a Hoàng Minh Thắng committed Mar 29, 2012
Showing with 53 additions and 30 deletions.
  1. +47 −28 src/leiningen/noir_gen.clj
  2. +6 −2 test/leiningen/test/noir_gen.clj
View
@@ -7,48 +7,67 @@
(->file (str "./resources/public/css/bootstrap.css") (slurp-resource (str "css/bootstrap.css")))
(->file (str "./resources/public/css/default.css") (slurp-resource (str "css/default.css"))))
-(defn generate [namespace entity fields database]
- (let [pages (render (slurp-resource (str "templates/entity_pages.clj"))
- {:namespace namespace,
- :entity entity,
- :entity-title (make-title entity),
- :entity_path (path-to-entity entity),
- :param (parameterize-entity entity),
- :fields fields})
- templates (render (slurp-resource (str "templates/entity_templates.clj"))
- {:namespace namespace,
- :entity entity,
- :entity_path (path-to-entity entity),
- :param (parameterize-entity entity),
- :fields fields})
- model (render (slurp-resource (str "templates/entity_model.clj"))
- {:namespace namespace,
- :entity entity,
- :fields fields})
- db (render (slurp-resource (str "templates/db.clj"))
+(defn print-help []
+ (println "help string"))
+
+(defn copy-resources []
+ (->file (str "./resources/public/css/bootstrap.css") (slurp-resource (str "css/bootstrap.css")))
+ (->file (str "./resources/public/css/default.css") (slurp-resource (str "css/default.css"))))
+
+(defn crud-setup [namespace database]
+ (let [db (render (slurp-resource (str "templates/db.clj"))
{:namespace namespace,
:database database})
srv (render (slurp-resource (str "templates/srv.clj"))
{:namespace namespace})
default (render (slurp-resource (str "templates/default.clj"))
{:namespace namespace})]
- (->file (str "./src/" namespace "/views/" (path-to-entity entity) "_pages.clj") pages)
- (->file (str "./src/" namespace "/views/" (path-to-entity entity) "_templates.clj") templates)
- (->file (str "./src/" namespace "/models/" (path-to-entity entity) "_model.clj") model)
(->file (str "./src/" namespace "/db.clj") db)
(->file (str "./src/" namespace "/server.clj") srv)
(->file (str "./src/" namespace "/views/default.clj") default)
(copy-resources)))
+(defn crud-view [namespace entity fields]
+ (let [field (map #(hash-map :name %) fields)]
+ (let [pages (render (slurp-resource (str "templates/entity_pages.clj"))
+ {:namespace namespace,
+ :entity entity,
+ :entity-title (make-title entity),
+ :entity_path (path-to-entity entity),
+ :param (parameterize-entity entity),
+ :fields field})
+ templates (render (slurp-resource (str "templates/entity_templates.clj"))
+ {:namespace namespace,
+ :entity entity,
+ :entity_path (path-to-entity entity),
+ :param (parameterize-entity entity),
+ :fields field})]
+ (->file (str "./src/" namespace "/views/" (path-to-entity entity) "_pages.clj") pages)
+ (->file (str "./src/" namespace "/views/" (path-to-entity entity) "_templates.clj") templates))))
+
+(defn crud-model [namespace entity fields]
+ (let [field (map #(hash-map :name %) fields)]
+ (let [model (render (slurp-resource (str "templates/entity_model.clj"))
+ {:namespace namespace,
+ :entity entity,
+ :fields field})
+ ]
+ (->file (str "./src/" namespace "/models/" (path-to-entity entity) "_model.clj") model))))
+
(defn noir-gen
"Create CRUD-pages for a Noir project.
Uses the default namespace.
Options which you may set in project.clj:
:noir-gen {:namespace my_namespace, :database my_database}"
- ([project entity & fields]
- (let [namespace (or (:namespace (:noir-gen project)) (:group project))
- database (or (:database (:noir-gen project)) (:group project))]
- (generate namespace entity (map #(hash-map :name %) fields) database))
- (println (str "The entity '" entity "' was successfully generated!"))
+ [project task & args]
+ (let [namespace
+ (or (:namespace (:noir-gen project)) (:group project))
+ database
+ (or (:database (:noir-gen project)) (:group project))]
+ (condp = task
+ "setup" (crud-setup namespace database)
+ "model" (crud-model namespace (first args) (next args))
+ "view" (crud-view namespace (first args) (next args))
+ (print-help)))
(println "Remember adding dependency to your 'project.clj' for congo-mongo like that:")
- (println "[congomongo \"0.1.7\"]")))
+ (println "[congomongo \"0.1.7\"]"))
@@ -3,6 +3,10 @@
(:use [clojure.test])
(:use [midje.sweet]))
+;to see the result, run $lein midje then look around this project directory. git-cola is a good tool for watching
(facts
-0
-=> 0)
+(crud-model "namespace" "parent.sub" ["f1" "f2"])
+=> "stupid string"
+(crud-view "namespace" "parent.sub" ["f1" "f2"])
+=> "stupid string"
+)

0 comments on commit fc9c9e8

Please sign in to comment.