Last active
August 29, 2015 14:10
-
-
Save jamesdavidson/7b4d962e19d418715200 to your computer and use it in GitHub Desktop.
A monkey-patch for debugging mongo criteria thingies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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