This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$().ready(function () { | |
var T = {}; | |
T.compile = function (template) { | |
var compile = Handlebars.compile(template), | |
render = { | |
render: function (ctx) { | |
return compile(ctx); | |
} | |
}; | |
return render; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 .= '…' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |