Skip to content

Instantly share code, notes, and snippets.

View werelax's full-sized avatar

Elías Alonso werelax

View GitHub Profile
@werelax
werelax / silly_benchmark.rb
Created February 8, 2012 17:11
Just some bad redis/filesystem benchmark for loging data
require 'benchmark'
require 'json'
require 'redis'
def rand_string(len)
rand(36**len).to_s(36)
end
random_data = Array.new(1000000).map do
{ field1: rand_string(100), field2: rand_string(100), field3: rand_string(100), field4: rand(10000) }
@werelax
werelax / lexer.js
Last active December 18, 2015 11:39
The laziest Lisp lexer in history.
function lexer(code) {
var strings = [],
normalize = function(code) {
return code.replace(/\(/g, " ( ")
.replace(/\)/g, " ) ")
.replace(/\s\s*/g, " ")
.replace(/^\s*/g, "")
.replace(/\s*$/g, "");
};
code = code.replace(/(".*?")/g, function(str) { return "ç" + (strings.push(str) - 1) + "ç"; });
@werelax
werelax / backbone-cacheable-mixin.js
Last active December 19, 2015 17:28
Mixin to make R.Models and R.Collections (backbone.js classes wraped in R classes) act as singletons with cached fetch. Very useful for high latency scenarios when you whant a simple way to reuse the AJAX data without too much trobule. Very simple to adapt for localStorage persistent caching.
var Cacheable = {
mixed: function(klass) {
var klassInit = klass.prototype.init || function() {},
superFetch = klass.prototype.fetch;
/* Need to decorate the method by hand */
klass.prototype.init = function() {
console.log(klass._instance)
if (!klass._instance) {
klass._instance = (klassInit.apply(this, arguments) || this);
@werelax
werelax / gist:6083011
Created July 25, 2013 19:34
La consulta de Ángel
{type: "image", data: {}}
{type: "post", data: {}}
var Element = Backbone.Model.extend({
initialize: function(options) {
},
subtypes: {
"image": Image,
"post": Post
},
@werelax
werelax / snippet.js
Created September 16, 2013 14:19
Backbone.View Mixins
var Votable = {
mixed: function(klass) {
var events = {
"click .js-votes-plus": "plusVote",
"click .js-votes-minus": "minusVote",
},
proto = klass.prototype
proto.events = _.extend({}, events, proto.events)
},
plusVote: function(e) {
@werelax
werelax / backbone-R-integration.js
Last active December 23, 2015 04:39
Backbone+R base classes
/* Base classes */
var BaseView = R.extend(Backbone.View, {
init: function() {
return Backbone.View.apply(this, arguments)
},
render: function() {
var model = (this.model || this.collection),
data = model.toJSON(),
html = this.template(data)
@werelax
werelax / models.js
Created September 16, 2013 14:22
Quick Collection/Model initialization hack with Solipsist.Factory
/* Collections */
var PostCollection = R.extend(BaseCollection, {
model: Post,
/* DEBUG */
fetch: function(options) {
var PostFactory = Solipsist.Factory({
id: Solipsist.Factory.int_sequence(),
title: "Soy un post",
description: "Soy una descripción de un post",
@werelax
werelax / gist:6581484
Last active December 23, 2015 04:39
Backbone Router con "pestañas" (secciones en una misma PageView con rutas distintas)
/* Router */
var AppRouter = R.extend(SmartRouter, {
init: function() {
this._super()
this.hottest()
},
routes: {
"": "index",
"latest": "latest",
@werelax
werelax / location-distance.js
Created October 26, 2013 16:16
Para el hackaton de tizen
distance: function(coords) {
var barPos = this.get("location"),
R = 6371*1000,
dtr = Math.PI / 180,
lat1 = coords.latitude*dtr, lon1 = coords.longitude*dtr,
lat2 = barPos.latitude*dtr, lon2 = barPos.longitude*dtr,
x = (lon2-lon1) * Math.cos((lat1+lat2)/2),
y = (lat2-lat1),
d = Math.sqrt(x*x + y*y) * R;
@werelax
werelax / AnimatedRegion.js
Last active December 26, 2015 19:29
pageslide.css y regiones custom: Slide y Flip
var AnimatedRegion = Base.Region.extend({
show: function(view, dir) {
this.forward = !dir; // true or false
Base.Region.prototype.show.call(this, view);
},
animationClass: "slide",
open: function(view) {
this.el = this.$el.get(0);
var from = this.currentView,
anim = this.animationClass,