Skip to content

Instantly share code, notes, and snippets.

View jwilger's full-sized avatar

John Wilger jwilger

View GitHub Profile
@jwilger
jwilger / my_given_driver.rb
Created February 25, 2012 23:24
TestData's @DaTa is a Hash that defaults to empty Hashes?
class MyGivenDriver < Kookaburra::GivenDriver
def a_bar(name)
bar_data = test_data.default(:bar)
record = api.create_bar(bar_data)
test_data.set_bars(name, record)
end
def a_foo(name, bar_name)
foo_data = test_data.default(:foo)
@jwilger
jwilger / ttt.rb
Created May 10, 2012 01:01
Tic-Tac-Toe in Ruby
#!/usr/bin/env ruby
def winner_is(winner)
puts "*Time passes...*"
if winner == :computer
puts "I win!"
else
puts "OK, you win this one."
end
end
def my_method(options = {})
observer = options.delete(:observer)
x = SomeObservable.new
x.add_observer(*observer)
end
# works with
my_method(:observer => some_object)
# and also with
@jwilger
jwilger / wishful_thinking.rb
Created June 26, 2012 22:37
I really wish I could do this in Ruby
class X
def connect(action, to, with_success_status_code)
:result_a
end
def connect(different_thing_entirely)
:result_b
end
end
@jwilger
jwilger / translate.rb
Created July 20, 2012 18:49
TDA, no mutation, no (used) return values between objects
class Printer
def initialize(options)
@out = options.fetch(:out)
@translator = options.fetch(:translator)
end
def say_hello
@translator.translate("Hello!", self)
end
@jwilger
jwilger / wtf.rb
Created July 28, 2012 06:32
Sometimes I Hate Ruby
require 'delegate'
class Foo
def select
'Foo#select'
end
end
class Bar < SimpleDelegator
def a
@jwilger
jwilger / api_options.rb
Created September 15, 2012 21:34
Quick API design poll
# A) Instantiate Connection object directly, have it know whether it
# needs to use a "test mode" implementation internally.
my_api = MyAPI::Connection.new(:mode => :test)
# B) Directly instantiate a "test mode" connection object. In
# production, a call to `MyAPI::Connection.new` would be used instead.
my_api = MyAPI::TestConnection.new
# C) The MyAPI module has a factory method that knows whether to return
# a `Connection` or a `TestConnection` (or a `Connection` in test
@jwilger
jwilger / example.rb
Last active January 4, 2016 20:49
Good idea for eliminating nil checks?
require 'pass_to'
class Page
attr_reader :content
def initialize(content = nil)
@content = content
end
def print
@jwilger
jwilger / 1 - Avoiding Multiple Render Errors by Using Throw and Catch.md
Created November 24, 2015 20:48
Avoiding Multiple Render Errors by Using Throw/Catch

Avoiding Multiple Render Errors by Using Throw/Catch

When creating Rails controllers, I often dislike the use of filters such as #before_filter due to the fact that they can get a bit unweildy. I'd much rather be able to look at the definition of an action's method and see right in the first few lines if there are is anything that might run which would cause the action to behave differently.

The nice thing about before-filters, of course, is that you can cancel the running of the action method form within the filter, which is handy for redirects or changing what is rendered in cases such as failed authorization, etc. If you use inline methods to, say, check authorization, and that method does a render or a redirect, you need some way to ensure that the remainder of the action does not execute, so that you avoid both performing unauthorized actions and getting a

@jwilger
jwilger / pseudo_singleton.rb
Last active June 20, 2016 18:04
PseudoSingleton
require 'delegate'
require 'forwardable'
module PseudoSingleton
def instance
@instance ||= new
end
private