Steve Losh avatar Steve Losh committed 2bf9a78

Add def* title desugaring across the board.

Comments (0)

Files changed (6)

metrics-clojure-core/src/metrics/counters.clj

 (ns metrics.counters
-  (:use [metrics.utils :only (metric-name)])
+  (:use [metrics.utils :only (metric-name desugared-title)])
   (:import (com.yammer.metrics Metrics))
   (:import (com.yammer.metrics.core Counter)))
 
 
 
 (defmacro defcounter
-  "Define a new Counter metric with a given title, or group, type and title."
-  ([title]
-   `(def ~title (counter ~(str title))))
-  ([group type title]
-   `(def ~title (counter ~(vec (map str [group type title]))))))
+  "Define a new Counter metric with the given title."
+  [title]
+  (let [[s title] (desugared-title title)]
+    `(def ~s (counter ~title))))
 
 
 ; Read ------------------------------------------------------------------------

metrics-clojure-core/src/metrics/gauges.clj

 (ns metrics.gauges
-  (:use [metrics.utils :only (metric-name)])
+  (:use [metrics.utils :only (metric-name desugared-title)])
   (:import (com.yammer.metrics Metrics))
   (:import (com.yammer.metrics.core Gauge)))
 
   The rest of the arguments may be a body form or function to call to
   retrieve the value of the Gauge."
   [title & [b & bs :as body]]
-  (if (and (empty? bs)
-           (symbol? b)
-           (fn? (eval b)))
-    `(def ~title (gauge-fn ~(str title) ~b))
-    `(def ~title (gauge ~(str title) ~@body))))
+  (let [[s title] (desugared-title title)]
+    (if (and (empty? bs)
+             (symbol? b)
+             (fn? (eval b)))
+      `(def ~s (gauge-fn ~title ~b))
+      `(def ~s (gauge ~title ~@body)))))
 
 
 ; Read ------------------------------------------------------------------------

metrics-clojure-core/src/metrics/histograms.clj

 (ns metrics.histograms
-  (:use [metrics.utils :only (metric-name get-percentiles)])
+  (:use [metrics.utils :only (metric-name get-percentiles desugared-title)])
   (:import (com.yammer.metrics Metrics))
   (:import (com.yammer.metrics.core Histogram MetricName)))
 
 (defmacro defhistogram
   "Define a Histogram metric with the given title."
   [title & args]
-  `(def ~title (histogram ~(str title) ~@args)))
+  (let [[s title] (desugared-title title)]
+    `(def ~s (histogram ~title ~@args))))
 
 
 ; Read ------------------------------------------------------------------------

metrics-clojure-core/src/metrics/meters.clj

 (ns metrics.meters
-  (:use [metrics.utils :only (metric-name)])
+  (:use [metrics.utils :only (metric-name desugared-title)])
   (:import (com.yammer.metrics Metrics))
   (:import (com.yammer.metrics.core Meter))
   (:import (java.util.concurrent TimeUnit)))
 (defmacro defmeter
   "Define a new Meter metric with a given title and event type."
   [title event-type]
-  `(def ~title (meter ~(str title) ~event-type)))
+  (let [[s title] (desugared-title title)]
+    `(def ~s (meter ~title ~event-type))))
 
 
 ; Read ------------------------------------------------------------------------

metrics-clojure-core/src/metrics/timers.clj

 (ns metrics.timers
-  (:use [metrics.utils :only (metric-name get-percentiles)])
+  (:use [metrics.utils :only (metric-name get-percentiles desugared-title)])
   (:import (com.yammer.metrics Metrics))
   (:import (com.yammer.metrics.core Timer MetricName))
   (:import (java.util.concurrent TimeUnit)))
 (defmacro deftimer
   "Define a new Timer metric with the given title."
   [title]
-  `(def ~title (timer ~(str title))))
+  (let [[s title] (desugared-title title)]
+    `(def ~s (timer ~title))))
 
 
 ; Read ------------------------------------------------------------------------

metrics-clojure-core/src/metrics/utils.clj

             [(parse-name metric-name)
              metric])]
     (into {} (map parse-entry (.allMetrics (Metrics/defaultRegistry))))))
+
+(defn desugared-title
+  "Syntactic sugary goodness for defining metrics concisely with macros.
+
+  Returns a vector of: [symbol-to-define MetricName]"
+  [mn]
+  (cond
+    (string? mn) [(symbol mn) mn]
+    (symbol? mn) [mn (str mn)]
+    :else [(symbol (last mn))
+           (map str mn)]))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.