Skip to content

Instantly share code, notes, and snippets.

View ianterrell's full-sized avatar

Ian Terrell ianterrell

  • University of Virginia
  • Charlottesville, VA
View GitHub Profile
@ianterrell
ianterrell / BDD.swift
Last active August 29, 2015 14:02
Rspec style matchers for Swift
import XCTest
protocol Matchable : Equatable, Comparable { }
class Matcher<T : Matchable> {
let subjectWrapper: T[]
var subject: T {
get {
return subjectWrapper[0]
function execute_loop() {
var vmfunc, pathtab, path;
var pathstart, pathend;
if (resumefuncop) {
//qlog("### at resume time, storing value " + resumevalue + " at funcop " + resumefuncop.key);
store_operand_by_funcop(resumefuncop, resumevalue);
resumefuncop = null;
resumevalue = 0;
}
@ianterrell
ianterrell / gist:111115
Created May 13, 2009 16:38
Implementation for Authorization plugin
# This lets you define user roles as methods,
# i.e. checking the role of "admin" on a user
# will delegate to user.admin?
#
# This is helpful because in ActiveRecord, boolean
# attributes automatically have their query method
# defined; a boolean field "admin" will define
# the "admin?" method for you.
#
# If your logic is more complex, you can write your
class Book extends ActiveRecordModel {
static $belongs_to = array(
array('publisher'),
array('author', 'readonly' => true, 'select' => 'name', 'conditions' => "name != 'jax'"),
array('another', 'class_name' => 'SomeModel')
);
static $has_many = array(
array('revisions'),
array('editors', 'through' => 'revisions')
class Book < ActiveRecord::Base
belongs_to :publisher
belongs_to :author, :select => [:id, :name], :conditions => "name != 'jax'"
belongs_to :another, :class_name => "SomeModel"
has_many :revisions
has_many :editors, :through => :revisions
has_one :something
[13:20][ian@ian-terrells-macbook-pro:~/src/test/i18ntest]$ ./script/console
Loading development environment (Rails 2.3.2)
>> o = Order.create
=> #<Order id: 1, created_at: "2009-05-25 17:21:00", updated_at: "2009-05-25 17:21:00">
>> t = OtherThing.create
=> #<OtherThing id: 1, created_at: "2009-05-25 17:21:05", updated_at: "2009-05-25 17:21:05">
>> o.payments.create
=> #<Payment id: 1, payable_id: 1, payable_type: "Order", created_at: "2009-05-25 17:21:11", updated_at: "2009-05-25 17:21:11">
>> o.payments.create
=> #<Payment id: 2, payable_id: 1, payable_type: "Order", created_at: "2009-05-25 17:21:13", updated_at: "2009-05-25 17:21:13">
[14:04][ian@ian-terrells-macbook-pro:~]$ ruby --version
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
[14:04][ian@ian-terrells-macbook-pro:~]$ irb
>> class X
>> end
=> nil
>> class Y < X
>> end
=> nil
>> X.subclasses
Loading development environment (Rails 2.3.2)
>> class Z < User; end
=> nil
>> User.subclasses
NoMethodError: protected method `subclasses' called for #<Class:0x22ce974>
from (irb):2
>> User.send(:subclasses)
=> [Z(id: integer, email: string, crypted_password: string, salt: string, created_at: datetime, updated_at: datetime, remember_token: string, remember_token_expires_at: datetime, activation_code: string, activated_at: datetime, state: string, deleted_at: datetime, editor: boolean, has_promise: boolean, reset_code: string)]
>>
@ianterrell
ianterrell / Installing Postgres Gems on OS X
Created December 16, 2010 00:11
Installing Postgres Gems on OS X
PATH=$PATH:/Library/PostgreSQL/9.0/bin/ env ARCHFLAGS="-arch x86_64" gem install pg
@ianterrell
ianterrell / redis-rb safety patch.rb
Created January 6, 2011 20:09
Monkey patch for safety using flushall and flushdb
require 'redis'
class Redis
alias :__flushall :flushall
alias :__flushdb :flushdb
def flushall(confirm=false)
if confirm
__flushall
else