Skip to content

Instantly share code, notes, and snippets.

@jamesdavidson
Last active August 29, 2015 14:10
Show Gist options
  • Save jamesdavidson/7b4d962e19d418715200 to your computer and use it in GitHub Desktop.
Save jamesdavidson/7b4d962e19d418715200 to your computer and use it in GitHub Desktop.
A monkey-patch for debugging mongo criteria thingies
class String
def fix(size, padstr=' ')
self[0...size].ljust(size, padstr)
end
end
class Mongoid::Criteria
# pull values out into easy-to-read columns
def interrogate(*syms)
syms = [:id,:created_at,:updated_at] if syms.empty?
puts syms.map{|sym| [sym] + self.map(&sym) }.transpose.map{|l| l.map{|l|l.to_s.fix(25)}.join("\t")}
end
# when you just want to #find on something that isn't an id
def just_one(args)
criteria = self.where(args)
case criteria.count
when 0
raise 'not found'
when 1
criteria.first
else
raise 'multiple found'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment