Skip to content

Instantly share code, notes, and snippets.

View kristjan's full-sized avatar

Kristján Pétursson kristjan

View GitHub Profile
@kristjan
kristjan / examples.rb
Created May 22, 2010 01:16
Some rspec macros to verify Rails model validations
# Require fields.
it_should_require :wish, :author, :title, :body, :signature
# Validate string lengths
it_should_validate_strings :body => 1024, :title => 255, :signature => 25
# Strings can be absent. Default length is 255, like Rails.
it_should_validate_strings :story => 10000,
:blurb => {:optional => true},
:tagline => {:optional => true}
@kristjan
kristjan / feb29_spec.rb
Created July 29, 2010 23:45
Tests that a Birthday can handle Feb 29 properly
describe "February 29" do
freeze_time
before(:each) do
@birthday = new_birthday(:month => 2, :day => 29, :year => 1984)
end
it "knows it's a jerk" do
@birthday.should be_feb_29
end
@kristjan
kristjan / gist:702789
Created November 17, 2010 00:30
Helpers to use Rails image_paths in CSS background images
module AssetsHelper
def background_images(images)
content_for :head do
content_tag(:style, :type => 'text/css') do
images.map{|selector, path| css_background_image(selector, path)}.
join("\n")
end
end
end
@kristjan
kristjan / copy.rb
Created November 17, 2010 06:26
Idea for a copy/asset skinning framework
module Copy
def self.for(wish)
Copy.const_get(@wish.class.name.demodulize).new(@wish)
end
# These methods are used by all Copy packages
class CopyPackage
def initialize(wish)
@wish = wish
@kristjan
kristjan / of.rb
Created January 11, 2011 22:48
Handles what you meant when you said [rand]*3
class Fixnum
def of
result = []
self.times {|i| result << yield(i)}
result
end
end
@kristjan
kristjan / count_friends.js
Created January 20, 2011 23:40
Count the number of friends you've selected in a FB inviter
function updateTotalSelected(form) {
var totalSelected = getTotalSelected(form);
var elem = getElementsByClassName(form, 'current_selected', 'span')[0];
elem.setTextValue(totalSelected)
}
function getTotalSelected(form) {
var serializedForm = form.serialize();
var ids = serializedForm.ids;
if (!ids) {
return 0;
@kristjan
kristjan / gist:794397
Created January 25, 2011 02:09
Poor man's smart autotest
export FILE=file/to/test.rb
export MOD=$(stat -c %Y $FILE | cut -d ' ' -f1)
while sleep .1
do
NEWMOD=$(stat -c %Y $FILE | cut -d ' ' -f1)
if [ $NEWMOD -gt $MOD ]
then
echo Changed $MOD $NEWMOD
export MOD=$NEWMOD
ruby $FILE
@kristjan
kristjan / gist:805168
Created February 1, 2011 00:40
A little something to prevent hanging greps
#!/usr/bin/env ruby
query = $*.shift
if $*.empty? && $stdin.tty?
puts "You haven't given grep anything"
else
cmd = %(grep "#{query}" #{$*.join(' ')})
puts `#{cmd}`
end
@kristjan
kristjan / api_formats.rb
Created February 28, 2011 06:35
DRYer XML/JSON response logic
module ApiFormats
API_FORMATS = %w[json xml].map(&:to_sym).freeze
def self.included(base)
base.send :private, :api_formats
end
def api_formats(format, data=nil, options={})
API_FORMATS.each do |encoding|
format.send(encoding) do
@kristjan
kristjan / .vimrc
Created March 2, 2011 20:26
Yank from a remote vim into OSX's local copy buffer
map <Leader>y :echo system('echo -n '.shellescape(@").' \| npaste')<CR>