Skip to content

Instantly share code, notes, and snippets.

View jasonneylon's full-sized avatar

Jason Neylon jasonneylon

View GitHub Profile
@jasonneylon
jasonneylon / creating-address-component.js
Last active October 22, 2015 16:47
Creating React address component
// The props for the React component are initally set from the server side
const addressConfig = #{@address_config.to_json};
// the component is rendered inside the form at a marker div embedded in the server generated HTML
const addressInstance = React.render(
React.createElement(Address, addressConfig),
document.getElementById("#billing_address")
);
@jasonneylon
jasonneylon / schema-validate.cljx
Created November 25, 2014 21:23
Schema validate sample
(s/validate
Data
{:a {:b "abc"
:c 123}
:d [{:e :bc
:f [12.2 13 100]}
{:e :bc
:f [-1]}]})
;; Success!
@jasonneylon
jasonneylon / schema.cljx
Created November 25, 2014 21:22
Schema data example
(def Data
"A schema for a nested data type"
{:a {:b s/Str
:c s/Int}
:d [{:e s/Keyword
:f [s/Num]}]})
@jasonneylon
jasonneylon / question.cljx
Created November 25, 2014 21:21
Example question map
[{:key :email-consent
:type :yes-no
:tags [#{:application/energy-offline}]
:schema (shared/map-has {:email-consent shared/Yes-No})
:group groups/account
:label "Would you like E.ON to email you account management reminders?"
:help-text "E.ON may also use these details to write to you about your account once your switch is complete."}]
@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 #"/")
@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)))
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 / 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"
@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_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