Skip to content

Instantly share code, notes, and snippets.

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 .= '…'
}
@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
@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 / 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 / fabricators.rake
Last active December 21, 2015 05:49
Rake task to list the class and names of fabricators defined via the Fabrication gem. Use CLASS and FABRICATOR to filter. Now part of fabrication gem: https://github.com/paulelliott/fabrication/issues/187
# https://gist.github.com/sshaw/6259749
#
# As of 2.9.4, this is part of the fabrication gem (less env vars)
# https://github.com/paulelliott/fabrication/blob/master/lib/tasks/defined_fabricators.rake
desc "List fabricators, use CLASS and FABRICATOR to filter"
task :fabricators => :environment do
Fabrication::Support.find_definitions if Fabrication.manager.empty?
if Fabrication.manager.schematics.none?
@sshaw
sshaw / github-grep.sh
Last active December 27, 2015 14:59
`git grep` that outputs github links with line numbers
#!/bin/bash
error()
{
echo "$*" >&2
exit 1
}
github="https://github.com"
remote="origin"
@sshaw
sshaw / gh-link.el
Last active December 27, 2015 20:59
Create a URL for a buffer's location in its GitHub repository based on current line or active region. Moved to: https://github.com/sshaw/git-link
@sshaw
sshaw / pg_cast.rb
Created March 15, 2014 16:19
PostgreSQL casting in Rails using the x::typename construct and ActiveRecord's where method
# Experiment, only tested under Rails 3.2.16, not sure how useful it really is...
# "Works" only with where conditions using a Hash
#
# Example:
# User.where :created_at::date => "2014-01-01" # :created_at is a timestamp
#
module PgCast
# All of these are not really viable
TYPES = %w[any char abstime aclitem anyarray anyelement anyenum anynonarray bigint bit bit varying boolean box bytea character character varying cid cidr circle cstring date double precision gtsvector inet int2vector integer internal interval language_handler line lseg macaddr money name numeric oid oidvector opaque path point polygon real record refcursor regclass regconfig regdictionary regoper regoperator regproc regprocedure regtype reltime smallint smgr text tid time with time zone time without time zone timestamp with time zone timestamp without time zone tinterval trigger tsquery tsvector txid_snapshot unknown uuid void xid xml]
@sshaw
sshaw / gist:141bcea8e8362bc2321a
Last active August 29, 2015 14:02
Iterate over array-like collections in Mojolicious without worrying about references or undef
use Mojo::Base -strict;
use Mojo::Collection;
use Mojo::Util 'monkey_patch';
use Data::Dump 'dd';
monkey_patch 'Mojo::Util',
c => sub { Mojo::Collection->new(Mojo::Util::array(@_)) },
array => sub {
my @data = ( @_ == 1 && ref($_[0]) eq 'ARRAY' ? @{$_[0]} : @_ );