Skip to content

Instantly share code, notes, and snippets.

View r00k's full-sized avatar
💭
Working on Tuple (https://tuple.app)

Ben Orenstein r00k

💭
Working on Tuple (https://tuple.app)
View GitHub Profile
@r00k
r00k / gist:3334214
Created August 12, 2012 20:27
Composition vs Inheritance

My favorite composition vs. inheritance metaphor:

There are only two types of relationships in an object model: composition and inheritance. Both have analogs in a family tree. A composition relation is like a marriage between objects. It is dynamic, it happens during the participating objects' lifetimes, and it can change. Objects can discard partners and get new partners to collaborate with. Inheritance relations are more like births into the family. Once it happens, it is forever. Just as both marriage and ancestry appear in the same family tree, composition and inheritance coexist in a single object model.

(from Object Design - Roles, Responsibilities and Collaborations)

# This works, right now:
class EmailCaptureCampaign < ActiveRecord::Base
has_many :campaign_merchants, :as => :campaignable, :dependent => :destroy
has_many :merchants, :through => :campaign_merchants
end
class Merchant < ActiveRecord::Base
has_many :campaign_merchants, :dependent => :destroy
has_many :campaigns, :through => :campaign_merchants, :source => :campaignable, :source_type => 'EmailCaptureCampaign'
class HumanizedTagContext
def initialize(game, context)
@game = game
@context = context
end
def to_s
case @context
when "location"
@r00k
r00k / gist:3105024
Created July 13, 2012 13:58
Dependency Injections Pros/Cons/Questions

Dependency Injection

Pros

  • Classes are more modular, as they depend only on the interface of passed-in dependencies. Class behavior can be changed by swapping out a new component.
  • Testing is simplified, since stubs can be substituted for any dependency.

Cons

  • It's harder to understand how a class works when reading just that class. You may have to track down its invocation to see what kind of components are passed in.
class User
def charge_for_subscription
PaymentActions.new(user, BraintreeGemSomethingSomething.new).charge_for_subscription
end
end
class PaymentActions
def initialize(user, braintree)
@user, @braintree = user, braintree
end
hardstatus alwayslastline "%{= KW}%-w%{= ck}%50>%n %t%{-}%+w%<%-22=%{w}%c [%l]"
nonblock 5
# detach the screen if we get disconnected
autodetach on
# Show messages at the bottom for only 0.5s
msgwait 0.5
@r00k
r00k / speaker.md
Created June 18, 2012 22:07 — forked from matiaskorhonen/speaker.md
Frozen Rails Talk Proposal Template (http://2012.frozenrails.eu/)

[Ben Orenstein]

Contact details

Speaker bio

class ClaimApplier
def initialize(order, claims)
@order = order
@claims = claims
end
def apply
@claims.sort_by(&:expires_at).each do |claim|
apply_claim(claim)
end
@r00k
r00k / gist:2226918
Created March 28, 2012 15:04
A function for opening the currently-focused jasmine spec in the browser
function! OpenJasmineSpecInBrowser()
let filename = expand('%')
let url_fragment = substitute(filename, "spec/javascripts", "evergreen/run", "")
let host_fragment = "http://localhost:3000/"
let url = host_fragment . url_fragment
silent exec "!open ~/bin/chrome" url
endfunction
subscription_ids_of_users_billed_next_month = ["2znd7r", "4cm6x6", "4pjyn6", "5sjmhr", "bk8bpb", "3hfmx6", "48k4jw", "57ffpw", "7f8yyb", "9fs8n6", "54vsh6", "5kk2x6", "6b28jw", "gtvm8w", "8fqqjb", "g7kydb", "3zv28w", "7mpvpw", "fzms8b", "2vzhnr", "cdyyb2", "54v2m2", "cxwx62", "km59g2", "2zg5xr", "3p7m4b", "6s7q2m", "76b6m2", "85mg4b", "94wqmm", "9ndr4b", "9tj2hr", "c2ncwm", "cdvyqg", "dd687r", "f82ysr", "fsdw22", "h6bk2m", "hcrt7r", "hjw4n6", "j5xsbm", "k8nks6", "kfrw6m", "3w9y36", "dw2c62", "gfrctb", "k4t4gm", "jmb976", "b5pyn6", "j5j26m", "9yb222", "j52dsr"]
subscription_ids_of_users_billed_this_month = ["5brqxr", "j25stb", "jng6m2", "2hw8w2", "bcbzs6", "5kmjqg", "87f762", "cgxwx6", "dg86fg", "fypcyw", "hztk76", "jwk6hr", "jzyqg2", "kmp7dw", "dx7xkg", "hvcx8b", "bvqp7r", "567gtw", "5xyhkg", "bbj8db", "hw6ygm", "4v77bm", "jc76pb", "24bqmm", "7nkhbm", "9vf2m2", "jk3d7r"]
next_month_users = User.all.select { |u| subscription_ids_of_users_billed_next_month.include?(u.braintree_subscription_token) }
this_month_