Skip to content

Instantly share code, notes, and snippets.

@robbielamb
Created November 30, 2009 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbielamb/245678 to your computer and use it in GitHub Desktop.
Save robbielamb/245678 to your computer and use it in GitHub Desktop.
class Sweatshop
self.record_map = Hash.new {|h,k| h[k] = Hash.new {|h,k| h[k] = 0}}
def self.record(klass, name, instance)
# self.record_map[klass][name.to_sym] << instance
self.record_map[klass][name.to_sym] += 1
instance
end
def self.pick(klass, name)
# self.record_map[klass][name.to_sym].pick || raise(NoFixtureExist, "no #{name} context fixtures have been generated for the #{klass} class")
offset = (rand * self.record_map[klass][name.to_sym]).to_i + 1
klass.first(:offset => offset) || raise(NoFixtureExist, "no #{name} context fixtures have been generated for the #{klass} class")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment