Skip to content

Instantly share code, notes, and snippets.

View pedroteixeira's full-sized avatar

Pedro Henriques dos Santos Teixeira pedroteixeira

View GitHub Profile
@pedroteixeira
pedroteixeira / underscore-ext.js
Created April 24, 2012 22:03
Crates buffered function
// Calls a function only once within the given wait time. Last call is
// considered, which is the difference between from _.throtle
//
_.buffer = function(func, wait, scope) {
var timer = null;
return function() {
if(timer) clearTimeout(timer);
var args = arguments;
timer = setTimeout(function() {
@pedroteixeira
pedroteixeira / jruby_yaml_path.rb
Created July 26, 2012 23:07
psych patch jruby 1.6.7
module Psych
class << self
alias_method :old_dump, :dump unless method_defined?(:old_dump)
def dump(o, io = nil, options = {})
old_dump(o, io, options).force_encoding('utf-8')
end
end
end
@pedroteixeira
pedroteixeira / select2.js
Created August 3, 2012 19:55
select2 + twitter bootstrap
var onscroll = function() {
var open = $(".select2-drop.select2-drop-active.select2-with-searchbox");
var originalTop = open.data('original-top');
var top = originalTop || parseInt(open.css('top'), 10);
if(!originalTop) {
open.data('original-top', top);
}
//ajust top
open.css('top', (top + $(window).scrollTop()) + 'px');
@pedroteixeira
pedroteixeira / form.rb
Created August 10, 2012 18:27
Dynamic class for Formtastic
class Form
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
# Subclass may provide a types hash. Any attributes not listed will
# default to string.
# self.types = {
# :description => :text,
# :state => {:type => :string, :limit => 2}
@pedroteixeira
pedroteixeira / controller.rb
Created August 29, 2012 17:26
dynamic date+time acessors a datetime attribute in rails model
# and you can handle formating in your controller
def action
params[:start_at] = params[:start_at_date] + ' ' + params[:start_at_time]
params.delete :start_at_date
params.delete :start_at_time
end
@pedroteixeira
pedroteixeira / console.rb
Created September 2, 2012 20:55
tip to test rails helper in console
class H; include MyHelper; end; h = H.new
@pedroteixeira
pedroteixeira / gp.clj
Created September 24, 2012 01:08
gaussian process regression
(ns demo.gp
(:use (incanter core stats)))
(set! *warn-on-reflection* true)
(def X (matrix [-4 -3 -1 0 2]))
(def Y (matrix [-2 0 1 2 -1]))
(def X* (matrix (range -5 5 0.2040816)))
@pedroteixeira
pedroteixeira / robot.js
Created December 6, 2012 18:50
Zolmeister
var Robot = function(robot){
robot.turnLeft(robot.angle % 90);
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
if (robot.parentId) {
robot.ahead(1);
robot.turnGunRight(1);
}
else {
@pedroteixeira
pedroteixeira / onbeforeunload.js
Last active December 27, 2015 17:39
onbeforeunload support for js navigation (via hashchange, such as backbone.history, etc)
var cancelNavigate;
$(window).on("hashchange", function(ev) {
if(cancelNavigate) {
cancelNavigate = false;
return false;
}
if(window.onbeforeunload) {
var ok = confirm(window.onbeforeunload());
if(ok) {
window.onbeforeunload = null;
@pedroteixeira
pedroteixeira / prelude-js.el
Last active August 29, 2015 14:14
web-mode is not indenting correctly javascript (web-mode in jsx files)
(custom-set-variables
'(web-mode-attr-indent-offset nil)
'(web-mode-code-indent-offset 2)
'(web-mode-markup-indent-offset 2)
'(web-mode-indentation-params '("lineup-calls" . nil))
)