Skip to content

Instantly share code, notes, and snippets.

@zenom
Created July 12, 2010 13:59
Show Gist options
  • Save zenom/472493 to your computer and use it in GitHub Desktop.
Save zenom/472493 to your computer and use it in GitHub Desktop.
class Domain
include MongoMapper::Document
# key <name>, <type>
key :domain, String, :required => true, :index => true
key :rate, Integer
key :max, Integer
key :deferred, Boolean, :index => true
timestamps!
# validate the domain name itself
validates_format_of :domain, :with => /\A((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :message => "You must enter a valid domain name."
# only validate if :deferred is not set.
validates_presence_of :rate, :if => lambda { not self.deferred? }
validates_presence_of :max, :if => lambda { not self.deferred? }
# make sure these are an integer, if deferred is not set.
validates_numericality_of :rate, :if => lambda { not self.deferred? }
validates_numericality_of :max, :if => lambda { not self.deferred? }
# if they are within these ranges its ok.
validates_inclusion_of :max, :within => 1..3600, :if => lambda { not self.deferred? }, :message => "Must be between 1 and 3600."
validates_inclusion_of :rate, :within => 1..3600, :if => lambda { not self.deferred? }, :message => "Must be between 1 and 3600."
# deferred items.
# Domain.deferreds.all
scope :deferreds, where(:deferred => true)
# find items where rate is great > x and < y
# Domain.by_rate(1200, 3600).all
scope :by_rate, lambda { |low, high| where(:rate.gte => low, :rate.lte => high) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment