Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam Lassek alassek

  • Flywheel
  • Omaha, NE
View GitHub Profile
View redis_mutex.rb
def Redis.Mutex(name, expires:, redis: Redis.current, &block)
raise ArgumentError, "block required" unless block_given?
if block.arity.zero?
success = block
block = proc do |on|
on.success(&success)
on.failure(&proc{})
end
end
View maybedoer.go
type Maybedoer struct {
err error
}
func (m Maybedoer) Maybe(f func() error) {
if m.err == nil {
c.err = f()
}
}
@alassek
alassek / db.rake
Created Jun 2, 2017
Rails db:refresh task
View db.rake
namespace :db do
desc "Do a clean reset of your DB from a backup file"
task refresh: [:environment, :check_protected_environments] do
filename = ARGV.last
jobs = ENV.fetch('JOBS', 2)
database = ActiveRecord::Base.connection.current_database
abort "Usage: rails db:refresh filename" if filename == 'db:refresh'
abort "Can't find: #{filename}" unless File.exists? filename
abort "Can't find pg_restore, make sure it is in your $PATH" unless system "which", "pg_restore"
View keybase.md

Keybase proof

I hereby claim:

  • I am alassek on github.
  • I am alassek (https://keybase.io/alassek) on keybase.
  • I have a public key ASCER3RZ4eL9jqSNbXVviFayarCOAT2JXki5SwZ6hhHopwo

To claim this, I am signing this object:

@alassek
alassek / 01_Annotated_Source.md
Last active Dec 4, 2020
Extending Arel to support @> operator
View 01_Annotated_Source.md

I've been doing some work lately with JSON documents in PostgreSQL using jsonb columns. You can query these documents using a GIN index in PG, but the syntax can be a little cumbersome

SELECT "events".* FROM "events" WHERE "events"."body" @> '{"shift":{"user_id":2}}'

You have to construct the right side of the query as a JSON string, which is a bit annoying. So I wondered if I could adapt Arel to do the tedious stuff for me.

View application_controller.rb
class ApplicationController < ActionController::Base
before_filter :require_authentication
private
def require_authentication
unless current_certificate.verify(public_key)
head :forbidden
end
end
@alassek
alassek / location.js
Last active Dec 10, 2015
window.location sucks
View location.js
/**
* `window.location` is a BAD api. Doesn't have a prototype, 'too much recursion' error
* if you try to inspect the constructor. Monkey-patching causes random disappearances
* of the monkey-patched function in Chrome, cloning causes 'too much recursion' in FF.
*
* This is what I'm reduced to. ಠ_ಠ
**/
(function () {
function Location () {
View html_truncate.rb
# By Henrik Nyh <http://henrik.nyh.se> 2008-01-30.
# Free to modify and redistribute with credit.
require "rubygems"
require "hpricot"
module TextHelper
# Like the Rails _truncate_ helper but doesn't break HTML tags or entities.
def truncate_html(text, max_length = 30, ellipsis = "...")
@alassek
alassek / extendEach.js
Created Sep 19, 2011
Easy multiple-inheritance in Backbone.js
View extendEach.js
(function () {
function extendEach () {
var args = Array.prototype.slice.call(arguments),
child = this;
_.each(args, function (proto) {
child = child.extend(proto);
});
@alassek
alassek / typeIntent.js
Created Sep 14, 2011
Trigger change event without hitting enter, like Google Live Search
View typeIntent.js
/*
* Copyright (c) 2011 Lyconic, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*