Skip to content

Instantly share code, notes, and snippets.

View timuruski's full-sized avatar
🥊
Hit. Don't get hit.

Tim Uruski timuruski

🥊
Hit. Don't get hit.
View GitHub Profile
require 'active_support/core_ext/array/extract_options'
# This makes it easy to tag all errors generated by the app with the
# CustomError module so they can be handled specifically. The NestedException
# module is also included so that it can wrap errors from other parts
# of the system.
module CustomError
# Public: Builds a new Error class and optionally includes the
# NestedException module.
#
@timuruski
timuruski / deploy_git_log.txt
Last active December 19, 2015 14:39
Git helper to generate formatted deploy message
# In your ~/.gitconfig
[alias]
deploy = "!source ~/.githelpers && deploy_git_log"
# Add to or create ~/.githelpers and chmod +x
#!/bin/bash
deploy_git_log() {
ref=${1:-'master'}
@timuruski
timuruski / README.md
Last active December 19, 2015 09:09
Not sure if this is a good idea, but it seems to work okay.

This is a callback object for copying denormalized associations from some sort of parent or key record into a child on initialize.

Consider this extremely simple multi-tenant blog application. We have many sites, and each site has a list of users. For our super-admin system we want some of these associations denormalized so has_many_through isn't quite what we need.

class Site < ActiveRecord::Base
@timuruski
timuruski / csv_parser.rb
Created May 27, 2013 16:41
CSV parser for working with malformed and mis-encoded CSV from a client.
require 'csv'
class CSVParser
FileEncodingProblem = Class.new(RuntimeError)
CSVParsingProblem = Class.new(RuntimeError)
def initialize(filepath)
@filepath = filepath
@errors = []
end
@stevedev
stevedev / gist:5628358
Created May 22, 2013 15:13
ActiveModel Filters idea

ActiveModel::Filters

Overview

Provide a ActiveModel::Validations style set of helpers for models to add input filters that work on all dirty fields before validation.

The point of this is I have a project that requires some data consistency and right now that's all ad-hoc and messy. I need a better way to quickly ensure that a username is lower case, things are trimmed properly before stored in the db, etc. Right now that's all done manually on the controller or model, or not at all.

Example Usage

set nocompatible " We're running Vim, not Vi!
set guifont=Bitstream\ Vera\ Sans\ Mono:h24
let g:molokai_original = 1
colorscheme molokai
syntax on " Enable syntax highlighting
filetype on " Enable filetype detection
filetype indent on " Enable filetype-specific indenting
@timuruski
timuruski / custom_behaviour_mixin.rb
Created January 14, 2013 23:31
An example of how to mix in custom behaviour based on a basic parent-child relationship. This could easily work with some sort of mixins column in the database or anywhere else. The technique should be applied lightly, but provides a means to add some custom behaviour to outlier cases but keeping it contained.
class Merchant < ActiveRecord::Base
has_one :store
# Returns a constantized namespace for the merchant.
# Not sure how to handle the nil case, if it can exist.
#
# Here we use GeneralMerchant, but it can be anything.
def namespace
read_attribute(:namespace).constantize rescue GeneralMerchant
end
@practicingruby
practicingruby / mrdi.md
Created December 19, 2012 22:29
Models, Roles, Decorators, and Interactions -- A modest proposal for a toned done version of DCI that isn't as janky as Concerns.

Models, Roles, Decorators, and Interactions

A modest alternative to DCI that might be worth further thought

One of the problems with advancing the discussion on DCI is that we lack a comparable alternative pattern that has the same goals, but favors a low ceremony approach. The closest thing we have to that is Rails concerns, but they are more like distant relatives of the DCI concepts rather than first cousins, and that makes comparisions between the two approaches not especially fruitful.

I am considering the idea of experimenting with my own paradigm that captures the intent and purity of DCI, but with the convenience of concerns. Please note that this is just the starting point of a conversation, it is NOT a promise of comercially available cold fusion or a cure for cancer. It's just a gist with an idea on it I'd like to hear your thoughts on.

What if we had a top-level topology that was split into Models, **Rol

@andkerosine
andkerosine / raskell.rb
Created August 15, 2012 05:56
Haskell-like list comprehensions in Ruby
$stack, $draws = [], {}
def method_missing *args
return if args[0][/^to_/]
$stack << args.map { |a| a or $stack.pop }
$draws[$stack.pop(2)[0][0]] = args[1] if args[0] == :<
end
class Array
def +@
@hellerbarde
hellerbarde / latency.markdown
Created May 31, 2012 13:16 — forked from jboner/latency.txt
Latency numbers every programmer should know

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs