Skip to content

Instantly share code, notes, and snippets.

View benvds's full-sized avatar
😀

Ben van de Sande benvds

😀
View GitHub Profile
@benvds
benvds / postgres-gem-install.sh
Created March 22, 2011 08:30
install postgres gem on os x 10.6
env ARCHFLAGS="-arch x86_64" gem install pg
@benvds
benvds / bootstrap-datepicker-dutch.js
Created July 4, 2012 12:12
Configure Bootstrap DatePicker for Dutch
(function() {
/* Update date_input plugin so that DD-MM-YYYY format is used. */
$.extend($.fn.datepicker.defaults, {
parse: function (string) {
var matches;
if ((matches = string.match(/^(\d{2,2})\-(\d{2,2})\-(\d{4,4})$/))) {
return new Date(matches[3], matches[2] - 1, matches[1]);
} else {
return null;
}
@benvds
benvds / deparam.js
Created October 22, 2012 09:51
Gets the params from the url and creates an object with the key/value pairs.
(function($) {
// Source: https://github.com/cowboy/jquery-bbq/blob/master/jquery.ba-bbq.js
//
// Section: Deparam (from string)
//
// Method: jQuery.deparam
//
// Deserialize a params string into an object, optionally coercing numbers,
// booleans, null and undefined values; this method is the counterpart to the
@benvds
benvds / test.rb
Last active December 10, 2015 15:18
Sequel dataset extension. For a reporting page query params can be given. These are used for the selects, joins, filters etc. I tried an approach where methods (like: apply_joins, apply_filters, etc.) passed the dataset around but this is cumbersome. Thinking you can also extend dataset it would prevent passing the dataset around. But now im hav…
module Stats
def select_metrics(metrics)
metrics.inject(self) do |ds, metric|
ds.select_metric(metric)
end
end
def select_metric(metric)
case metric
when 'total'
@benvds
benvds / test_rest_api.rb
Last active December 21, 2015 04:19
use of basic auth disables before block
require 'sinatra/base'
class TestRestApi < Sinatra::Base
before do
headers['Access-Control-Allow-Origin'] = '*'
content_type :json
end
# no more header & content_type when to following is enabled
/* global jQuery */
;(function(window, $) {
'use strict';
var firstPrivate = 1,
secondPrivate = '2',
defaults = {
'firstOption': false,
'secondOption': 5000
@benvds
benvds / guide-ror-on-ubuntu.md
Last active August 29, 2015 14:03
Step by step guide to installing a Ruby on Rails application on Ubuntu

Step by step guide to installing a Ruby on Rails application on Ubuntu

This guide aims to get you started running a basic Ruby on Rails application on Ubuntu. It can be used for web applications with small usage and provides a cheap alternative, e.g. €5 a month on Digital Ocean, to services like Heroku which cost much more because of expensive add-ons. The setup includes:

  • support for just a single Ruby on Rails web application
  • database support (PostgreSQL)
  • email support (sendmail)
  • background job support
  • multiple application processes (Unicorn)
  • front-end http server with asset caching (Nginx)

Creating a Javascript component – reasons & requirements

Recently I've created javascript calendar component called Kalender. What the hell was I thinking in creating yet another javascript calendar?

Reasons

Javascript has a lot of frameworks and libraries. Sadly a lot of them are not up to my personal standards. This is probably because about every developer seems to be writing Javascript. This includes junior developers and developers who mainly use another language and bend their style only enough to get things working. Besides that front-end development is a rapidly changing environment with browsers, frameworks and requirements changing fast, what used to be best practice is now frowned upon.

Most of my professional time is spent working on client projects. Clients expect me to deliver business value as effectively as possible which often means writing code that is good enough and using existing libraries, even if these libraries are not what I'd like them to

Kalender – Technical considerations

These are some technical considerations for the Kalender component. It returns a calendar matrix containing day objects for a given month. It's implementation is an experiment in writing a Javascript component in a functional style.

When starting I keep things in one file even when there are multiple modules in it. No need to split things up until the it grows to about 150 lines. After 150 lines it gets harder to keep clarity and concepts need to be separated, named and have it's own place.

At the start of the project I mindlessly added lodash, thinking I'd be really needing helper functions like reduce and contains. Turned out I'd could do without. This makes a big difference dependency wise, the library now only has dependencies for building and testing but none are required to get things running.

Removing reduce and contains forced me to think about data structures a bit more. I like objects as data struc

// rotate an array
const rotate = (amount, list) => {
const offset = -(amount % list.length);
return list.slice(offset).concat(list.slice(0, offset));
};