Skip to content

Instantly share code, notes, and snippets.

View afgomez's full-sized avatar
♻️

Alejandro Fernández Gómez afgomez

♻️
View GitHub Profile
@afgomez
afgomez / jquery.fn.js
Created June 11, 2013 14:56
quick template for jQuery plugins
(function($) {
$.fn.plugin = function() {
return this.each(function() {
var $this = $( this );
if ( !$this.data('plugin') ) {
$this.data( 'plugin', new Plugin(this) );
}
});
}
@afgomez
afgomez / random_spec.rb
Created June 6, 2013 16:18
Easy way to allow remote inspection in poltergeist/capybara
# spec/features/random_spec.rb
require 'spec_helper'
describe "Random feature" do
it "Does't have debugging" do
# ...
# page.driver.debug
end
@afgomez
afgomez / validate_spanish_id.js
Last active April 22, 2024 07:08
Spanish DNI, CIF, NIE validator
/**
* ValidateSpanishID. Returns the type of document and checks its validity.
*
* Usage:
* ValidateSpanishID( str );
*
* > ValidateSpanishID( '12345678Z' );
* // { type: 'dni', valid: true }
*
* > ValidateSpanishID( 'B83375575' );
inputs.each(function(){
var address = $(this),
form = address.closest('form'),
hiddenCoords = form.find('#coordinates');
// Namespaceamos el evento para quitarlo más adelante
form.on('submit.getAddress', function(e){
// Paramos el evento por defecto
e.preventDefault();
@afgomez
afgomez / jquery.last_tweet.js
Last active December 10, 2015 04:28
Get the last tweet from any user
/**
* jQuery.lastTweet
* ----------------
*
* Fetch the last tweet from the specified user
*
* Usage:
*
* $('#container').lastTweet('username');
*
@afgomez
afgomez / dmginstall.sh
Created November 29, 2012 22:29
Download and install a .dmg
#!/bin/bash
# Downloads and install a .dmg from a URL
#
# Usage
# $ dmginstall [url]
#
# For example, for installing alfred.app
# $ dmginstall http://cachefly.alfredapp.com/alfred_1.3.1_261.dmg
#
@afgomez
afgomez / sqlize.rb
Created February 27, 2012 12:14
Convert a hash into a valid key=value SQL string
# gives a valid key=value SQL string for using in "INSERT INTO `table` SET" sentences
# >> "INSERT INTO `table_name` SET #{sqlize(Model.find(id).attributes)};"
# returns
# => "INSERT INTO `table_name` SET `attr1`=value1,`attr2`=value2..."
# Handles different datatypes
def sqlize hash
hash.map do |k,v|
v = case v
when Time
@afgomez
afgomez / jqueyr.filterator.js
Created February 22, 2012 11:29
jQuery filterator
/**
* jQuery filterator
*
* Filter collections of elements using javascript.
* Uses HTML5 data attributes to get the filter values.
*
* Usage:
*
* $('element_containing_filters').filterator( { target: filter_criteria, items: selector_or_jquery_collection_to_filter } );
*
@afgomez
afgomez / wikistance.rb
Created July 2, 2011 19:59
Measures distance of any wikipedia article to Philosophy. Based on http://xkcd.org/903/ alt text
# WikiStance. A Wikipedia distance meter.
# Based on the alt text of http://xkcd.org/903/
# Gets a Wikipedia URL and measure the distance of this page to the Philosophy article, clicking on links not in parens
# neither italics
#
# Author:: Alejandro Fernández (mailto:antarticonorte@gmail.com)
# Copyright:: Copyright (c) 2011 Alejandro Fernández
# License:: GPL
#
# =Usage=
@afgomez
afgomez / colorize_git_ps1.bash
Created April 7, 2011 21:06
Color current git branch in prompt
# Color the current git branch in prompt
# red -> master branch, take care!
# blue -> just a normal branch
# green -> dev branch
# cyan -> feature branch
# magenta -> fix branch
function __colorize_git_ps1 () {
local branch=$(__git_ps1 "%s")
if [ -n "$branch" ]; then