Skip to content

Instantly share code, notes, and snippets.

; Thoughts on a query API for clj-record.
(require '[clj-record.query :as q)
(manufacturer/find-records {
:founded "1910" ; still just a simple equality
:grade (q/between 85 95)
:name (q/in "Ford" "GM")})
; where between and in return functions that know what they're about ...
(ns clj-record.query
(:require [clojure.contrib.str-utils :as str-utils]))
(defn- clause-and-parameters [join-with values]
[(str-utils/str-join join-with (reduce (fn [v1 v2] (conj v1 (if (nil? v2) "NULL" "?"))) [] values)) (filter (complement nil?) values)])
(defn between [value1 value2]
(fn [attribute]
(let [[clause parameters] (clause-and-parameters " AND " [value1 value2])]
# Allows you to specify the position of a column in your migrations' add_column and change_column calls.
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
def add_column_options!(sql, options)
super
if options[:after]
sql << " AFTER #{quote_column_name(options[:after])}"
elsif options[:first]
sql << " FIRST"
end
end
@duelinmarkers
duelinmarkers / gist:1528186
Created December 28, 2011 14:48
Weird Jruby error
A #&lt;Class:0x44e38467&gt; occurred in controller_name_was_here#show:
org.jruby.javasupport.JavaClass.installClassFields(JavaClass.java:843)
org.jruby.javasupport.JavaClass.installClassFields(JavaClass.java:843)
org.jruby.javasupport.JavaClass.setupProxy(JavaClass.java:710)
org.jruby.javasupport.Java.createProxyClass(Java.java:520)
org.jruby.javasupport.Java.getProxyClass(Java.java:449)
org.jruby.javasupport.Java.getInstance(Java.java:358)
@duelinmarkers
duelinmarkers / couchdb.rake
Created September 11, 2012 16:23
Migrations spike for RapidFTR
# in lib/tasks
namespace :couchdb do
task :migrate => :environment do
migration_files = Dir[Rails.root.join('db/migrate/*.rb')]
migration_files_by_id = Hash[*migration_files.map {|path| [migration_number_from_file(path), path] }.flatten]
applied_migration_ids = SchemaMigration.all.map(&:id)
migration_ids_to_apply = migration_files_by_id.except(*applied_migration_ids).sort
if migration_ids_to_apply.empty?
puts "DB already up to date."
else
@duelinmarkers
duelinmarkers / first.clj
Created November 5, 2012 22:19
Testing multi-file gist embedding
(ns example.first)
@duelinmarkers
duelinmarkers / app.clj
Created November 5, 2012 22:41
Handling file upload in a ring app
(ring.adapter.jetty/run-jetty
(ring.middleware.params/wrap-params
(ring.middleware.multipart-params/wrap-multipart-params
(fn [request]
(if (= :get (:request-method request))
(render-html-form-with-file-field-called "my-file")
(let [file (get-in req [:params "my-file" :tempfile])]
; file is a java.io.File
(with-open [reader (clojure.java.io/reader file)]
; reader is a--You guessed it!--java.io.Reader
@duelinmarkers
duelinmarkers / abstracting_the_details.clj
Created November 14, 2012 23:19
Asynchronous HTTP in Clojure with Aleph
(defn get-fb-access-token [code redirect-uri]
(run-pipeline (access-token-url code redirect-uri)
{:error-handler (fn [_])}
#(http-request {:url %})
read-access-token-from-response))
(defn get-fb-user-with-token [access-token]
(run-pipeline access-token
{:error-handler (fn [_])}
fb-user-url
@duelinmarkers
duelinmarkers / defmodel_macro.clj
Created November 27, 2012 19:46
"Why not to use my library clj-record" code examples
(defmodel widget db
(associations
(:has-many :sizes))
(validation
(:name "Name is required" #(not (empty? %)))))
module RapidFTR
module AddOns
def self.add_exporter(e)
exporters << e
end
def self.exporters
@exporters ||= []
end