Skip to content

Instantly share code, notes, and snippets.

@cainlevy
cainlevy / service_base.rb
Last active November 8, 2019 19:02
Simple base class for service objects that implement 1) an initializer and 2) a perform method that returns a Promise
# Service objects are classes that inherit from this base and implement two instance methods:
# * initialize(*args, **params) => void
# * perform() => Promise
class ServiceBase
include ActiveModel::Validations
# invokes the service and returns a Promise
def self.perform(*args, **params)
klass = params.any? ? new(*args, **params) : new(*args)
klass.perform_in_transaction
layout title description tags
default
SQL Style Guide
A guide to writing clean, clear, and consistent SQL.
data
process

Purpose

@cainlevy
cainlevy / mobiledoc.rb
Created April 30, 2018 22:05
parsing mobiledoc in ruby
require 'keyword_struct'
Mobiledoc = KeywordStruct.new(:version, :markups, :atoms, :cards, :sections) do
Markup = Struct.new(:tag, :attrs)
Atom = Struct.new(:name, :text, :payload)
Card = Struct.new(:name, :payload)
# Parses a serialized Mobiledoc into a collection of Ruby objects
def self.parse(str)
json = JSON.parse(str)
@cainlevy
cainlevy / mobiledoc031.d.ts
Last active March 28, 2018 21:49
Mobiledoc 0.3.1 as TypeScript
/**
* Mobiledoc 0.3.1
* https://github.com/bustlelabs/mobiledoc-kit/blob/master/MOBILEDOC.md#specification
*/
interface Mobiledoc {
version: '0.3.1';
markups: Markup[];
atoms: Atom[];
cards: Card[];
@cainlevy
cainlevy / helper.rb
Created July 21, 2017 01:13
catch_uniqueness_errors
# forgiveness is faster than permission
private def catch_uniqueness_errors
yield
rescue ActiveRecord::RecordNotUnique => e
field = e.cause.message.match(/index_([a-z_]*)_on_([a-z_]*)/)[2]
raise unless field
errors.add(field, :taken)
end
@cainlevy
cainlevy / README.md
Last active March 3, 2016 18:27
Checkbox Desync w/ Group Select

REPRO

  1. click Group
  2. DESYNC: notice that Group is unchecked and true

VARIATION: on change

  1. modify template to bind the action on="change"
  2. click Foo
  3. notice that Group is checked and true
@cainlevy
cainlevy / retries.md
Last active October 26, 2021 01:38
sidekiq retries reference

Sidekiq Retries

Want to use Sidekiq's exponential backoff, but give up sooner? This table will help you pick a retry number.

class MyWorker
  ...
  sidekiq_options retry: N
  ...
end
@cainlevy
cainlevy / gist:4504210
Created January 10, 2013 17:51
ruby constant lookups
#!/usr/bin/env ruby
X = 'hello'
module A
X = 'world'
end
module A::B
def self.tst
@cainlevy
cainlevy / queue_time_middleware.rb
Created November 28, 2011 23:00
Delete HTTP_X_QUEUE_START for requests with large content lengths.
class QueueTimeMiddleware
def initialize(app)
@app = app
end
def call(env)
if env['CONTENT_LENGTH'].to_i > 10000
env['HTTP_X_QUEUE_START'] = nil
end
@cainlevy
cainlevy / with_retries.rb
Created October 28, 2011 21:19
with_retries{} helper to easily catch and retry exceptions in Ruby
module Retriable
# This will catch any exception and retry twice (three tries total):
# with_retries { ... }
#
# This will catch any exception and retry four times (five tries total):
# with_retries(:limit => 5) { ... }
#
# This will catch a specific exception and retry once (two tries total):
# with_retries(Some::Error, :limit => 2) { ... }
#