Skip to content

Instantly share code, notes, and snippets.

@sshaw
sshaw / yymmdd.rb
Last active August 29, 2015 14:05
Small DSL for idiomatic date parsing and formatting in Ruby. => Moved to https://github.com/sshaw/yymmdd
require "date"
# Small DSL for idiomatic date parsing and formatting in Ruby.
# https://gist.github.com/sshaw/53c27b148e903a07e494
#
# Usage:
#
# include YYMMDD
# date = Date.today
#
@sshaw
sshaw / amazon-affiliate_url.rb
Last active August 29, 2015 14:04
Ruby Module to Generate Amazon Affiliate URLs for Products and Searches
require "uri"
#
# Generate Amazon Affiliate URLs for products and searches
#
# Amazon::AffiliateURL.config do |cfg|
# cfg.id = "your-id" # required
# cfg.domain = :us # optional, defaults to :us, can be a domain name
# cfg.category = "grocery" # optional, default category for search(),
# cfg.secure = true # optional, use https scheme or not, defaults to false
@sshaw
sshaw / qualified.rb
Last active August 29, 2015 14:03
Nokorigi HappyMapper Qualified/Unqualified Namespace Parsing
#
# Auto-generated by jaxb2ruby v0.0.1 on 2014-07-05 13:57:07 -0400
# https://github.com/sshaw/jaxb2ruby
#
require "happymapper"
module Com module Example module Person
@sshaw
sshaw / gist:5799c2fc39c226823e29
Last active August 29, 2015 14:02
Syntax for creating directory trees
# /a/b.txt
# /a/c.txt
# /a/d/e/
# /a/d/f.txt
:a => [
"b.txt",
"c.txt",
:d => [ "e/", "f.txt" ]
]
@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]} : @_ );
@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 / 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 / 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 / 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?