Skip to content

Instantly share code, notes, and snippets.

@cupakromer
cupakromer / v1.rb
Last active December 25, 2015 17:39
MetaProgram some factories
module ModelFactory
def self.factory(resource, defaults = {})
resource = resource.downcase
defaults = defaults.dup.freeze
resource_defaults = "default_#{resource}_attributes".to_sym
build_resource = "build_#{resource}".to_sym
create_resource = "create_#{resource}".to_sym
define_method resource_defaults do
@cupakromer
cupakromer / gist:6996373
Created October 15, 2013 18:31
expect backtic
class FooBar
def itsends
`ls`
end
end
describe "Stuff" do
it do
fb = FooBar.new
expect(fb).to receive(:`).with('ls')
@cupakromer
cupakromer / group_by_behavior.rb
Last active December 24, 2015 22:09
different ways of skinning tests
describe 'Something Awesome' do
context 'checking on the user' do
context 'given a guest user' do
subject(:guest) { create(:guest_user) }
it 'sees nothing'
end
context 'given an admin user' do
# Ruby's World
# Runs this code
puts self # => main
# Runs this code
module ChangeMyWorld
# The Worls is now changed
@cupakromer
cupakromer / gist:6800292
Created October 2, 2013 20:46
Mathz is hard

Problem

Setup

Given a one-way function h(vi) => hi mapping visitors to unique anonymous tokens, for a set of visitors, v0, v1, ..., vn ∈ V, over a time period T, where t0, t1, ..., tm ∈ T are discreet units. Determine how many unique hi are in a subset of T: d ⊂ T, assuming you have a mapping l(hi) => tj of hi to the last tj it was seen at.

Issue

Given the above with mapping ∀ ti ∈ T to ui, calculate the new unique count for a period of k・d.

@cupakromer
cupakromer / gist:6766137
Created September 30, 2013 16:09
Example of using StringIO
describe 'Your stuff here' do
context "when doing something fun" do
it "let's the world know" do
io = StringIO.new
logger = Logger.new(io)
activity = Activity.new(logger: logger)
expect{ activity.play }.to change{ io.string }.to include "Having so much fun!"
end
end
@cupakromer
cupakromer / gist:6718222
Last active December 24, 2015 00:39
Route dir
AcmeInc::Application.routes.draw do
resources :accounts # /spec/routes/accounts_routing_spec.rb
resources :users # /spec/routes/users_routing_spec.rb
resources :widgets # /spec/routes/widgets_routing_spec.rb
resources :shippers do
# All of these tests would go in /spec/routes/shippers_routing_spec.rb
member do
get 'scans', :to => 'shippers#member_scans'
end
@cupakromer
cupakromer / gist:6717218
Created September 26, 2013 17:07
Proposed RSpec Rails matcher for response checks
class RespondWithMatcher < RSpec::Matchers::BuiltIn::BaseMatcher
def initialize(scope, expected, message = nil)
@expected = case expected
when Symbol, Fixnum
expected
when String
expected.to_sym
else
raise ArgumentError, "Unexpected type #{expected} must be symbol or Fixnum"

Bearded's Hourly Contract

Date: [[Date of Document]] Between [Our Company] and [Your Company]

Summary

We’re not big on formality, but sometimes it’s best to have a few simple things written down so that we’re all on the same page. In this contract you won’t find complicated legal terms or large passages of unreadable text. We have no desire to trick you into signing something that you might later regret. We do want what’s best for the safety of both parties, now and in the future.

@cupakromer
cupakromer / gist:6072860
Last active December 20, 2015 04:39
CFP Proposal: Cooking Code

Cooking Code

Using Refactorings as Smells to Cook Aromatic Code

Refactoring is a huge part of the programming workflow. Over the past year there has been a resurgence of the refactoring practice. With so much emphasis on the process and techniques of refactoring, the bigger picture is often missed.

When you refactor, you should stop and ask: "Why do I need to do this? What's different now? Did a perception change, was something missed, etc.? How can I prevent this the next time?"

Remember, not all smells are "bad" and it's a subjective scale.