Skip to content

Instantly share code, notes, and snippets.

@sshaw
sshaw / gist:1984738
Created March 6, 2012 07:41
CGI::Expand in Mojolicious::Controller
use Mojo::Base 'Mojolicious';
use CGI::Expand;
sub startup
{
my $self = shift;
$self->hook(before_dispatch => sub {
my $c = shift;
my $hash = CGI::Expand->expand_hash($c->req->params->to_hash);
$c->param($_ => $hash->{$_}) for keys %$hash;
@sshaw
sshaw / gist:2009351
Created March 9, 2012 23:45
Mojolicious text_area change
my ($c, $name) = (shift, shift);
my $cb = sub {''};
if(@_ % 2) {
if(ref $_[-1] && ref $_[-1] eq 'CODE') {
$cb = pop;
}
else {
my $value = shift;
$cb = sub {$value};
@sshaw
sshaw / gist:2174243
Created March 23, 2012 19:44
Ember.js Chosen Multiple Select
// Tested with 1.0.0-pre.2, 0.9.7 or less does not work.
// Chosen modified to search on @results_data[n].text not .html
App.ChosenMultipleSelect = Em.Select.extend({
multiple: true,
attributeBindings: [ 'multiple' ],
didInsertElement: function() {
this._super();
this.$().chosen();
},
@sshaw
sshaw / Mojolicious::Plugin::IncludeStatic.pm
Created March 29, 2012 07:25
Mojolicious Static Includes
sub register
{
my ($self, $app) = @_;
$self->helper(include_static => sub {
my ($c, $path) = @_;
return unless $path;
if($path !~ m|^/|) {
my $root = first { -f "$_/$path" } @{$c->app->renderer->paths};
@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));
@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: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: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: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: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 |