Skip to content

Instantly share code, notes, and snippets.

@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]} : @_ );
@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 / 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 / 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 / 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 / gist:1cd69d078cffeebc76cd
Created August 25, 2014 03:05
Install Ruby SBDB on OS X
brew install berkeley-db4
gem install sbdb -- --with-db-lib=/usr/local/opt/berkeley-db4/lib --with-db-include=/usr/local/opt/berkeley-db4/include/
@sshaw
sshaw / q.rb
Last active August 29, 2015 14:05
SuckerPunch + GDBM: Don't think GDBM can be used with 2 writers...
require "gdbm"
require "sucker_punch"
class IdBasedJob
include SuckerPunch::Job
def perform(db, ids)
ids.each do |id|
SuckerPunch.logger.info(sprintf "Running %s [%d]\n", id, Thread.current.object_id)
if do_some_thang
@sshaw
sshaw / bh-flash.gemspec
Last active August 29, 2015 14:08
Render Flash Messages Using Ruby's Bootstrap Helpers (Bh)
Gem::Specification.new do |s|
s.name = "bh-flash"
s.version = "0.0.2"
s.date = "2015-08-02"
s.summary = "Render flash messages using Bootstrap Helpers"
s.description =<<-DESC
Render flash messages using Bootstrap Helpers: http://fullscreen.github.io/bh/
Inspired by: https://github.com/planetargon/flash-message-conductor
DESC
s.authors = ["Skye Shaw"]
@sshaw
sshaw / gist:386070
Created May 1, 2010 05:29
Rails current_page? Style Referer Function
# Usage:
# referer? :controller => 'beezies', :action => 'holla', :type => 'popozuda'
# referer? %r|/\d+/edit|
def referer?(options)
referer = request.headers["Referer"]
return (options.blank? ? true : false) if referer.blank?
if options.is_a?(Regexp)
referer =~ options