Skip to content

Instantly share code, notes, and snippets.

View sporto's full-sized avatar

Sebastian Porto sporto

  • Melbourne, Australia
View GitHub Profile
@sporto
sporto / gist:1787176
Created February 10, 2012 06:26
Javascript Boilerplate Structure with Closures
//define a global object where to put all our code
//this is an immediate calling function
window.APP = (function(){
//create an object that will be returned
var that = {};
//private methods
function init(){
APP.registrationController.init();
APP.galleryController.init();
@sporto
sporto / gist:3614806
Created September 3, 2012 23:29
Rivets adapter for CanJS
rivets.configure({
adapter: {
subscribe: function(obj, keypath, callback) {
callback.wrapped = function(ev, attr, how, newVal, oldVal) { callback(newVal) };
obj.bind('change', callback.wrapped);
},
unsubscribe: function(obj, keypath, callback) {
obj.unbind('change', callback.wrapped);
},
read: function(obj, keypath) {
@sporto
sporto / gist:4281464
Created December 14, 2012 00:39
How to access a subquery in an active record association object?
# we have a query like this one:
# but in some cases it will return duplicate records because of the joins
subquery = Workspace
.joins(:workspace_roles)
.where('workspace_roles.role_id IN (?)', role_ids)
.joins(:workspace_locations => :location)
.where('workspace_locations.location_id IN (?)', location_ids)
.where('locations.active = 1')
# on in order not to return duplicates we do the following:
@sporto
sporto / gist:5059974
Last active December 14, 2015 08:48
Semantic versioning seems broken to me, would you please correct me?

Semantic versioning seems broken to me, would you please correct me?

When you start a new project everything is perfectly logical, 0.x means that it is not stable and the api might change. Then you release 1.0 and the api is supposed to be stable. Then 1.1, 1.3 should just be incremetal changes.

But then you want to start the work on version 2. Version 2 will have some major changes and be incompatible with 1. Where is this work supposed to go?

Some options:

  • Put it in version 1.x, this is just wrong because 1.x is supposed to be an evolution from 1.0.
  • Start versioning from 0 again with a different name, e.g. Foo2 0.1, then when you are done rename it back to Foo 2.0.
@sporto
sporto / gist:5844051
Created June 23, 2013 06:41
Angular Nested Directives
<!doctype html>
<html ng-app='NESTED'>
<head>
<script src="components/angular/angular.js"></script>
<script src="js/app/nested.js"></script>
</head>
<body>
<div ng-controller="IndexCtrl">
<collection collection='tasks'></collection>
</div>
@sporto
sporto / client.go
Last active December 10, 2019 16:58
Concurrency: JavaScript vs Go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
@sporto
sporto / gist:6317306
Created August 23, 2013 09:27
Best practice on return values from module that handles http
var myModule = require('myModule');
myModule.fetch('some/url', function (err, val) {
//if 'some/url' cannot be found what should myModule return?
//some options I can think of
//#1
//err => null
//val => 404
@sporto
sporto / bubble.go
Last active December 22, 2015 06:38
Bubble sort
package main
import (
"fmt"
)
func bubble(s []int) {
for i := len(s) - 2; i >= 0; i-- {
for j := 0; j <= i; j++ {
if s[j] > s[j+1] {
@sporto
sporto / server.go
Created September 4, 2013 05:55
Go - Node - Memory usage
package main
import "net/http"
func main() {
bytes := make([]byte, 1024*1024)
for i := 0; i < len(bytes); i++ {
bytes[i] = 100
}
@sporto
sporto / gist:6618950
Last active June 7, 2017 14:31
Gems