Skip to content

Instantly share code, notes, and snippets.

View remvee's full-sized avatar

Remco van 't 🪶 remvee

  • 52°N, 5°E
View GitHub Profile
@remvee
remvee / sticky_input.js
Created April 20, 2011 08:18
Make values of input elements with class "sticky" reoccur when form opened again.
// All values of INPUT and TEXTAREA elements with the "sticky" class
// will be remembered in a session cookie and restored on page load
// when the "value" of that element is empty.
StickyInput = {
name: function(e) {
return "sticky-" + e.name;
},
set: function(e, v) {
@remvee
remvee / button_to_get.rb
Created April 5, 2011 09:40
automatically use a anchor button combination for button_to with get method
# Automatically use a anchor button combination for button_to with get method.
module ActionView::Helpers::UrlHelper
def button_to_with_getawareness(name, options = {}, html_options = {})
html_options = html_options.stringify_keys
unless html_options["method"].to_s.downcase == "get"
return button_to_without_getawareness(name, options, html_options)
end
html_options.delete("method")
@remvee
remvee / setup-misc.el
Created March 21, 2011 11:11
Paredit for ruby and javascript.
;; Enable paredit for a couple for non lisp modes; tweak
;; paredit-space-for-delimiter-predicates to avoid inserting spaces
;; before open parens.
(dolist (mode '(ruby espresso))
(add-hook (intern (format "%s-mode-hook" mode))
'(lambda ()
(add-to-list (make-local-variable 'paredit-space-for-delimiter-predicates)
(lambda (_ _) nil))
(enable-paredit-mode))))
@remvee
remvee / roman.clj
Created January 27, 2011 22:21
Translate from and to roman numerals
(ns roman [:use clojure.test])
(def num-to-dig {1 "I", 4 "IV", 5 "V", 9 "IX"
10 "X", 40 "XL", 50 "L", 90 "XC"
100 "C", 400 "CD", 500 "D", 900 "CM"
1000 "M"})
(def dig-to-num (into {} (map (fn [[k v]] [v k]) num-to-dig)))
(defn encode
@remvee
remvee / hash_highlight.js
Created January 25, 2011 12:39
Highlight the element referred to by the anchor on the URL.
// Highlight the element referred to by the anchor on the URL.
Event.observe(window, 'load', function() {
var id, hash = document.location.hash;
if (hash && (id = hash.replace(/^#/, '')) && (id = $(id))) {
setTimeout(function() {
(id.down('td') ? id.select('td') : [id]).each(function(e) {
new Effect.Highlight(e);
});
}, 250);
@remvee
remvee / classify_check_box.rb
Created January 17, 2011 10:49
Add checkbox class to checkboxes rendered by ActionView::Helpers::FormHelper
# Automatically add checkbox class to check_box form elements to allow
# styling them separately in older versions of IE.
module ActionView::Helpers::FormHelper
def check_box_with_class(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
check_box_without_class(object_name, method, {:class => "checkbox"}.merge(options), checked_value, unchecked_value)
end
alias_method_chain :check_box, :class
end
@remvee
remvee / validations.clj
Created January 16, 2011 16:39
Basic validation framework.
(ns #^{:author "Remco van 't Veer"
:doc
"Basic validation framework for maps. The constructed validator
functions take two arguments the before- and after state of the map.
They return a map of errors where keys are, typically, keys of the
input map and values are lists of keyworded errors or maps of keyword
errors to some relevant argument. For example:
((validator (not-blank :nr)
@remvee
remvee / attachment.rb
Created January 7, 2011 14:54
Make a file extension to mime-type map from /etc/mime.types
File.read("/etc/mime.types").split("\n").reject{|l| l =~ /^\s*#/}.reject(&:blank?).reduce({}){|m,l| t, *x = l.split(/\s+/); x.each{|k| m[k] = t}; m}
@remvee
remvee / stripped_attributes.rb
Created January 5, 2011 13:13
strip_value_of ActiveRecord column helper
class ActiveRecord::Base
class << self
attr_writer :stripped_attributes
def stripped_attributes
@stripped_attributes ||= []
end
def strip_value_of(*attrs)
self.stripped_attributes += attrs
@remvee
remvee / levenshtein.clj
Created December 31, 2010 13:51
Calculate levenshtein distance between two sequences.
(defn levenshtein
"Calculate levenshtein distance between two sequences."
{:test #(are [d s1 s2] (= d (levenshtein s1 s2))
0 "soup" "soup"
3 "kitten" "sitting"
3 "sitting" "kitten"
0 [1 2 3] [1 2 3]
1 [1 2 3] [1 2 3 4])}
[s1 s2]
(((reduce (fn [d [i j]]