Skip to content

Instantly share code, notes, and snippets.

View justinko's full-sized avatar

Justin Ko justinko

  • Colorado, USA
  • 20:35 (UTC -06:00)
  • LinkedIn in/jko170
View GitHub Profile
@justinko
justinko / gist:1916757
Created February 26, 2012 13:37
No instance variables in Rails controllers
class PostsController < ActiveRecord::Base
helper_method :posts, :post
def index; end
def create
if post.save
end
end
@justinko
justinko / ruby_code.rb
Created February 26, 2012 01:35
API Driven Design

Let's say I have a file that needs to be parsed and validated. Instead of jumping to tests, I want to first think about the objects involved and their messages.

I would then write out the above code.

After I'm satisfied with the objects and their interactions, I would then write a spec for FileObject or FileValidator#validate.

I consider this approach a technique above TDD. Before, when I used to jump straight into the specs, it would often result in working code, but bad design. TDD encourages you to think about design, but only in your head (which is obviously not as efficient as writing it out, not to mention the visual feedback).

NOTE: The very first thing I do is create a functional/high-level spec, so this approach only applies on the implementation level (units).

@justinko
justinko / gist:1894922
Created February 23, 2012 20:36
Help for rspec-users mailing list question
require 'spec_helper'
shared_examples_for 'employee signin' do
it { should have_selector('title', text: employee.emp_full_name) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
describe "Authentication" do
require 'spec_helper'
describe TCPSocket do
describe '#write_nonblock' do
let(:payload) { 'JUNK IN THE TUBES' }
it "writes if the operation won't block" do
setup_tcp do |client, peer|
client.write_nonblock(payload).should eq payload.length
peer.read(payload.length).should eq payload
class NullLogger < Logger
def initialize(*args); end
def write(*input); true; end
end
Rails.application.assets.logger = NullLogger.new
class TemporaryLoggerLevel
LEVEL = Logger::ERROR
@justinko
justinko / gist:1703386
Created January 30, 2012 08:43
Ruby 1.9 Delegator bug
require 'delegate'
A = Struct.new(:a)
class B < DelegateClass(A)
attr_accessor :foo
end
b = B.new(A.new(5))
puts b.inspect
puts b.respond_to?(:foo)
@justinko
justinko / gist:1702877
Created January 30, 2012 06:16
Ruby 1.8 Proc equality bug
a = lambda {}
b = a.dup
m = Module.new
a.extend m
b.extend m
puts a == b # true for 1.9, false for 1.8 :(
@justinko
justinko / gist:1684051
Created January 26, 2012 17:59
Methods to aid in testing without loading the Rails environment
def stub_model(model_name)
stub_class model_name, ActiveRecord::Base
end
def stub_class(class_name, super_class = Object)
stub_module class_name, Class.new(super_class)
end
def stub_module(module_name, object = Module.new)
module_name = module_name.to_s.camelize
@justinko
justinko / gist:1592044
Created January 11, 2012 00:01
rbx error
This change:
https://github.com/rubinius/rubinius/commit/15f8bdf440236481c5fcb953234b1e173edf9e2e
Breaks this code:
https://gist.github.com/1065789
Backtrace:
Kernel(NilClass)#each (method_missing) at kernel/delta/kernel.rb:81
Rubinius::AST::Container(Rubinius::AST::Script)#container_bytecode at \
/Users/justinko/.rvm/rubies/rbx-head/runtime/18/compiler/ast/definitions.rbc:944
@justinko
justinko / post_creation_context.rb
Created January 9, 2012 22:38
The value of contexts in DCI
class PostCreationContext
def initialize(post)
@post = post
@post.extend PostNotifier
@post.extend PostIndexer
@post.extend PostCacher
end
def execute
@post.save