Last active
September 23, 2015 23:11
-
-
Save raymcdermott/4a38c308561b9927c607 to your computer and use it in GitHub Desktop.
Minimal configurator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns car-configurator.core | |
(:require [schema.core :as s])) | |
; NO CODE YET ... Just data | |
;---> define some basic attributes | |
(def fuel (s/enum :diesel :petrol :hybrid)) | |
(def engine {:name s/Str :fuel fuel :volume s/Num :bhp s/Int}) ; c02, etc. | |
(def colour {:name s/Str :rgb {:red s/Int :green s/Int :blue s/Int} :hex s/Int}) ; metallic, etc. | |
; STILL NO CODE YET ... Just data | |
; sample engines | |
(def diesel-engine {:name "Diesel Engine" :fuel :diesel :volume 1.4 :bhp 100}) | |
(def petrol-engine {:name "Petrol Engine" :fuel :petrol :volume 1.6 :bhp 120}) | |
; STILL NO CODE YET ... Just data, really | |
; sample colours | |
(def standard-grey {:name "Standard Grey" :rgb {:red 142 :green 142 :blue 142} :hex 0x8E8E8E}) | |
(def sunrise-red {:name "Sunrise Red" :rgb {:red 255 :green 48 :blue 48} :hex 0xFF3030}) | |
; STILL NO CODE YET ... Just data, could come from a configuration file | |
;---> define model | |
(def model {(s/required-key :name) s/Str (s/required-key :engine) engine (s/required-key :colour) colour}) | |
; sample model | |
(def base-diesel {:name "Base Model Diesel" :engine diesel-engine :colour standard-grey}) | |
; STILL NO CODE YET ... data again, it could come from a database | |
;---> define option | |
(def option {(s/required-key :name) s/Str (s/optional-key :engine) engine (s/optional-key :colour) colour}) | |
; STILL NO CODE YET ... Just data, maybe from a client | |
; sample options | |
(def sports-option {:name "Sports Petrol" :engine petrol-engine :colour sunrise-red}) | |
(def diesel-sports-option {:name "Sports Diesel" :colour sunrise-red}) | |
; FINALLY ... the actual code - everything else, really is just data | |
(defn configure | |
"Merge the option map with a model map - possibly too simple but let's see how far it takes us" | |
[model option] | |
(merge model option)) | |
; Run the code | |
(def config1 (configure base-diesel sports-option)) | |
(def config2 (configure base-diesel diesel-sports-option)) | |
; Print the results | |
(clojure.pprint/pprint base-diesel) | |
(clojure.pprint/pprint config1) | |
(clojure.pprint/pprint config2) | |
;=> (clojure.pprint/pprint base-diesel) | |
;{:name "Base Model Diesel", | |
; :engine {:name "Diesel Engine", :fuel :diesel, :volume 1.4, :bhp 100}, | |
; :colour | |
; {:name "Standard Grey", | |
; :rgb {:red 142, :green 142, :blue 142}, | |
; :hex 9342606}} | |
;=> (clojure.pprint/pprint config1) | |
;{:name "Sports Petrol", | |
; :engine {:name "Petrol Engine", :fuel :petrol, :volume 1.6, :bhp 120}, | |
; :colour | |
; {:name "Sunrise Red", | |
; :rgb {:red 255, :green 48, :blue 48}, | |
; :hex 16724016}} | |
;=> (clojure.pprint/pprint config2) | |
;{:name "Sports Diesel", | |
; :engine {:name "Diesel Engine", :fuel :diesel, :volume 1.4, :bhp 100}, | |
; :colour | |
; {:name "Sunrise Red", | |
; :rgb {:red 255, :green 48, :blue 48}, | |
; :hex 16724016}} | |
; A little more OPTIONAL code which may be used in dev and / or prod to check the data against the schemas | |
(s/validate engine diesel-engine) | |
(s/validate engine petrol-engine) | |
(s/validate colour standard-grey) | |
(s/validate colour sunrise-red) | |
(s/validate model base-diesel) | |
(s/validate option sports-option) | |
(s/validate option diesel-sports-option) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment