Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created October 22, 2010 23:48
Show Gist options
  • Save yitsushi/641563 to your computer and use it in GitHub Desktop.
Save yitsushi/641563 to your computer and use it in GitHub Desktop.
[demo] Vaidator for size of an integer
class TestCucc
include MongoMapper::Document
key :cucc, Integer
validates_true_for :cucc,
:logic => lambda { | x | x.cucc >= 1 && x.cucc <= 5 },
:message => "must be in the following range: 1 to 5"
end
=begin
ruby-1.9.2-p0 > load "t.rb"
=> ["TestCucc"]
ruby-1.9.2-p0 > c = TestCucc.new
=> #<TestCucc _id: BSON::ObjectId('4cc2225b14f6fc3873000001'), cucc: nil>
ruby-1.9.2-p0 > c.cucc = 8
=> 8
ruby-1.9.2-p0 > c.save
=> false
ruby-1.9.2-p0 > c.errors
=> #<Validatable::Errors:0x95eff98 @errors={:cucc=>["must be in the following range: 1 to 5"]}>
ruby-1.9.2-p0 >
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment