Skip to content

Instantly share code, notes, and snippets.

View jasonneylon's full-sized avatar

Jason Neylon jasonneylon

View GitHub Profile
@jasonneylon
jasonneylon / gist:5676205
Created May 30, 2013 07:14
Watch the current values of a mongo document in the terminal
watch 'mongo app_db --eval "printjson(db.sessions.findOne({_id: ObjectId(\"51a62b06f9b19aac870000b1\")}))"'
@jasonneylon
jasonneylon / gist:6362994
Last active December 21, 2015 20:39
Get pairs of dates for every week in the last year
require 'active_support/all'
(12.months.ago.to_i..0.months.ago.to_i).step(1.week).to_a.push(Time.new).each_cons(2) do |from, to|
puts "From #{Time.at(from).to_s} to #{Time.at(to).to_s}"
end
OUTPUT
======
From 2012-08-28 08:11:55 +0100 to 2012-09-04 08:11:55 +0100
From 2012-09-04 08:11:55 +0100 to 2012-09-11 08:11:55 +0100
@jasonneylon
jasonneylon / gist:6455085
Created September 5, 2013 19:40
Two sided horizontal bar chart
<!DOCTYPE html>
<html>
<head>
<title>Bar Chart</title>
<script type="text/javascript" src="javascript/d3.v2.min.js"></script>
<style type="text/css">
.chart {
background: #00ccff;
margin: 10px;
@jasonneylon
jasonneylon / hash_functions.rb
Created May 9, 2014 09:56
Flatten the values of a hash where each value is one item array
# {:a => [1], :b => [2]} => {:a => 1, :b => 2}
def flatten_hash_values(h)
Hash[h.map {|m, i| [m, i.first]}]
end
@jasonneylon
jasonneylon / hash_extensions.rb
Created May 21, 2014 15:54
Implementation of Clojure get-in for Ruby hashes
class Hash
def get_in(*keys)
key = keys.first
value = self[key]
return value if keys.count == 1
return nil if keys.count > 1 and !value.respond_to? :get_in
return value.get_in(*keys.drop(1)) if key?(key)
end
end
@jasonneylon
jasonneylon / rename_collection.rb
Last active August 29, 2015 14:02
Method to rename a collection in mongo db which works even if the target collection exists
# version of rename collection that drops the existing collection if present.
# the standard rename method on the collection object provided by the driver
# throws an exception if the target collection exists
# http://docs.mongodb.org/manual/reference/command/renameCollection/
def self.rename_collection(db, old_name, new_name)
cmd = BSON::OrderedHash.new
database_name = db.name
cmd[:renameCollection] = "#{database_name}.#{old_name}"
cmd[:to] = "#{database_name}.#{new_name}"
cmd[:dropTarget] = true
@jasonneylon
jasonneylon / hash.rb
Created August 27, 2014 16:52
FlashHash patch for Rails 3
class Hash
def now
Rails.logger.warn "Stubbing now during upgrade"
{}
end
def keep
# stub keep for upgrade purposes
Rails.logger.warn "Stubbing keep during upgrade"
from json import load, JSONEncoder
from optparse import OptionParser
from re import compile
float_pat = compile(r'^-?\d+\.\d+(e-?\d+)?$')
charfloat_pat = compile(r'^[\[,\,]-?\d+\.\d+(e-?\d+)?$')
parser = OptionParser(usage="""%prog [options]
Group multiple GeoJSON files into one output file.
@jasonneylon
jasonneylon / macro.cljx
Last active August 29, 2015 14:10
Macro to optimise maps targeting ClojureScript
(defmacro defquestions
"Macro to reduce the size of the generated javascript by omitting unused keys (and their values) in the questions.
It works by looping over the questions and only retaining the keys that are required on the client side.
To ensure it is applied on the client size code require the macro and then apply it using #+cljs.
For the server side just use the #+clj marker and a normal def."
[name questions]
{:pre [(symbol? name) (and (coll? questions) (every? map? questions))]}
(let [filtered-questions (mapv #(select-keys % [:key :tags :schema :ask-if]) questions)]
`(def ~name ~filtered-questions)))
@jasonneylon
jasonneylon / dates.cljx
Created November 25, 2014 21:20
ClojureScript Date parsing
#+clj (defn parse-date
[date-string]
(f/parse (f/formatter "dd/MM/yyyy") date-string))
;; Javascript date uglyness: http://stackoverflow.com/questions/2587345/javascript-date-parse
#+cljs (defn parse-date
"Parses a UK date string such as 29/01/2014 and converts it into a JS date object or nil if invalid.
Handles bizarro javascript case such as new Date(2014, 1, 31) => Mon Mar 03 2014 00:00:00 GMT+0000 (GMT)"
[date-string]
(let [date-parts (clojure.string/split date-string #"/")