Skip to content

Instantly share code, notes, and snippets.

@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_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 / 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 / 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 / filter_links.js
Created December 11, 2012 13:50
Filter links by tag in MapReduce (Riak)
@mattsnyder
mattsnyder / key_filtering.rb
Created January 16, 2013 19:18
Filtering keys by date range in Riak
Riak::MapReduce.new(Ripple.client).
add(@bucket_to_filter,
[
[:tokenize, "-", 1], #Note: 1 based index
[:between, start_time.to_s(:key), end_time.to_s(:key)]
])
@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 / log_output.rb
Created April 9, 2013 16:04
Helpful method for logging output in Rake tasks
def log(string)
string.split(/\n/).each do |line|
puts "[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}] #{line}"
end
end
@mattsnyder
mattsnyder / rake_task_example.rb
Created April 9, 2013 16:15
Sample shell for a rake task with namespacing and benchmarking
namespace :b2b2dot0 do
namespace :magento do
desc "Imports some stuff from one place to another"
task :import_stuff => :environment do
benchmark = Benchmark.measure do
# The import stuff here
SomeStuff.import do |stuff|
stuff.do_something_more
end
end.real
@mattsnyder
mattsnyder / faster_csv.rb
Created April 26, 2013 21:04
add BOM for UTF8
class FasterCSV
def <<(row)
# make sure headers have been assigned
if header_row? and [Array, String].include? @use_headers.class
parse_headers # won't read data for Array or String
self << @headers if @write_headers
end
# Handle FasterCSV::Row objects and Hashes
row = case row