Skip to content

Instantly share code, notes, and snippets.

@sshaw
sshaw / random_country.rb
Last active December 20, 2015 04:09
Generate random country data (names and codes) in Ruby: ISO, IOC, FIFA and more
require "normalize_country" # need 0.1.0
module RandomCountry
@@cache = {}
@@selector = [].respond_to?(:choice) ? :choice : :sample # :choice is for Ruby < 1.9
def self.random(format)
@@cache[format] ||= NormalizeCountry.to_a(format)
@@cache[format].send(@@selector)
end
@sshaw
sshaw / gist:5794354
Last active December 18, 2015 13:59
Use Handlebars and/or EmberJs with Twitter's typeahead.js. Example: http://jsfiddle.net/sshaw/TuQmH/
$().ready(function () {
var T = {};
T.compile = function (template) {
var compile = Handlebars.compile(template),
render = {
render: function (ctx) {
return compile(ctx);
}
};
return render;
@sshaw
sshaw / rack_test_flash.rb
Last active December 14, 2015 01:59
Rack::Test assertion for the Rack/Sinatra/Padrino flash
# Rack::Test assertion for the Rack/Sinatra/Padrino flash
# https://gist.github.com/sshaw/5010741
# assert_match flash[:error], /Péssimo|Ótimo/
# Or...
# assert_flash_is_set :error => /Péssimo|Ótimo/
# assert_flash_is_set :error => "Ótimo Foninha!"
# assert_flash_is_set "Ótimo Foninha!"
# assert_flash_is_set :error
sub maxwords {
my ($s, $n) = @_;
return unless $s and $n > 0;
my @words = split /\s+/, $s;
my $t = join ' ', @words[0..$n-1];
if(@words > $n) {
$t =~ s/[[:punct:]]$//;
$t .= '&hellip;'
}
@sshaw
sshaw / gist:3121370
Created July 16, 2012 07:33
MySQL Update via Swap
mysql> create table t (id int, v1 int, v2 int);
Query OK, 0 rows affected (0.05 sec)
mysql> insert into t values (1,100,200),(2,101,201),(3,102,202);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from t;
+------+------+------+
| id | v1 | v2 |
@sshaw
sshaw / gist:3114566
Created July 15, 2012 02:45
HTML Paragraph Helper for Mojolicious
# Turn text from a text area into HTML paragraph elements.
$self->helper(paragraphs => sub {
my ($c, $text) = @_;
return unless $text;
my $html = join '', map $c->tag('p', $_), split /^\s*\015\012/m, $text;
Mojo::ByteStream->new($html);
});
@sshaw
sshaw / gist:3094651
Created July 12, 2012 00:18
Single or Plural Count Helper for Mojolicious
# Single or Plural Count Helper for Mojolicious using Lingua::EN::Inflect::PL
# usage:
# $size = 10
# count($size, 'user')
# 10 users
#
# $users = an array ref with one User object
# count($users)
# 1 User
#
@sshaw
sshaw / gist:2779490
Created May 24, 2012 04:45
perl-module-version-after-point
; Need to remove font lock stuff before printing
(defun perl-module-version-after-point ()
(interactive)
(save-excursion
(if (search-forward-regexp "\\([A-Za-z]\\w*\\(::\\w+\\)*\\)" (point-at-eol) t)
(let* ((module (match-string 1))
; syntax for cmd.exe and *nix
(command (format "perl -e\"eval qq|require %s|; print %s->VERSION unless qq|$@|\"" module module))
(version (shell-command-to-string command)))
(message "%s: %s" module (if (string= version "") "unknown" version)))
@sshaw
sshaw / gist:2355239
Created April 10, 2012 22:47
Locale::Country::Multilingual Benchmark
# Oops, here's the right benchmark snippet.
use Locale::Country::Multilingual;
use List::MoreUtils qw(zip pairwise);
use Benchmark 'timethese';
use Unicode::Collate;
my $lcm = Locale::Country::Multilingual->new;
my $c = Unicode::Collate->new;
my ($lang, $format) = qw(en_US LOCALE_CODE_ALPHA_2);
@sshaw
sshaw / gist:2298052
Created April 4, 2012 05:34
Mojolicious Twitter Bootstrap Flash/Stash Alerts
use Mojo::ByteStream;
app->helper(alerts => sub {
my $c = shift;
my $html = '';
for my $message (qw|success info error|) {
my $css = "alert alert-$message";
if($c->flash($message)) {
$html .= $c->tag('div', class => $css, $c->flash($message));