Skip to content

Instantly share code, notes, and snippets.

@mattsnyder
mattsnyder / 2i_key_range_query.rb
Created January 16, 2013 19:20
2i query to retrieve records by key range
MyModel.find_by_index('$key', '20121001'..'20130117')
@mattsnyder
mattsnyder / filter_links.js
Created December 11, 2012 13:50
Filter links by tag in MapReduce (Riak)
@mattsnyder
mattsnyder / ripple_matchers.rb
Created September 25, 2012 12:46
Custom matchers for testing Ripple Documents
RSpec::Matchers.define :be_a_ripple_document do
match do |doc|
doc.class.included_modules.include?(Ripple::Document) ||
doc.class.included_modules.include?(Ripple::EmbeddedDocument)
end
failure_message_for_should do
"#{actual.class.to_s} should be a Ripple::Document"
end
@mattsnyder
mattsnyder / phone_number.rb
Created August 23, 2012 20:23
Phone Number Generator
def phone_number
o = [(0..9)].map{|i| i.to_a}.flatten;
(0..9).map{ o[rand(o.length)] }.join
end
@mattsnyder
mattsnyder / roman_numeral_calculator_spec.rb
Created July 20, 2012 13:03
Example of classname in description block
describe RomanNumeralCalculator do
describe "with simple conversions" do
Then { RomanNumeralCalculator.convert(1).should == 'I' }
end
end
@mattsnyder
mattsnyder / roman_numeral_custom_matcher.rb
Created July 20, 2012 12:41
Example custom matcher to use for testing roman numeral conversions
RSpec::Matchers.define :be_converted_to do |expected|
match do |actual|
@result = Calculator.convert(actual)
@result == expected
end
failure_message_for_should do
"Expected #{actual} to be converted to #{expected}, but got #{@result}"
end
@mattsnyder
mattsnyder / converting_roman_numerals_spec.rb
Created July 20, 2012 12:45
example specs for converting roman numerals
describe "Converting integers to roman numerals" do
describe "with simple conversions" do
Then { 1.should be_converted_to 'I' }
Then { 5.should be_converted_to 'V' }
Then { 10.should be_converted_to 'X' }
end
describe "with additive conversions" do
Then { 3.should be_converted_to 'III' }
@mattsnyder
mattsnyder / roman_numeral_calculator_describe_block.rb
Created July 20, 2012 12:32
Example rewrite of describe block
describe "Converting integers to roman numerals" do
end
@mattsnyder
mattsnyder / Prefer.rb
Created July 8, 2011 13:16
Offer up a replacement should the prefered value not be present
def Prefer(value)
if value.present? then
value
else
block_given? ? yield : NullObject.new
end
end
@mattsnyder
mattsnyder / authentication_helpers.rb
Created June 17, 2011 14:07
Easy faking of user authentication in RSpec when using Devise
module AuthenticationHelpers
# Example usage: login mock_user(Editor)
def login(user)
request.env['warden'] = mock(Warden,
:authenticate => user,
:authenticate! => user)
end
def mock_user(type, stubs={})
mock_model(type, stubs).as_null_object