Created
May 15, 2010 15:17
-
-
Save techarch/402242 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 15-minutes with IronRuby and MongoDB | |
# ------------------------------------ | |
# Prerequisites: | |
# 1) Download and extract the IronRuby zip from http://ironruby.net/download to C:\ironruby-1.0v4 | |
# 2) Add the C:\ironruby-1.0v4\bin folder of your IronRuby installation folder to your PATH system environment variable | |
# | |
# 3) Download and extract the MongoDB for Windows zip from http://www.mongodb.org/display/DOCS/Downloads to C:\MongoDB | |
# 4) Create a C:\data\db directory | |
# 5) Start the mongod.exe from the bin folder of your MongoDB installation folder | |
# | |
# ------------------------------------ | |
# 6) Open a command prompt window | |
# 7) Install the MongoMapper ruby gem and its prerequisites: | |
# ir -S gem install mongo_mapper --no-rdoc --no-ri | |
# | |
# 8) Start IR from a command prompt | |
# 9) Type: require 'mongodb-ir-test.rb' | |
# 10) Review the output of the test and the code below and continue experimenting | |
require 'rubygems' | |
gem 'mongo', '= 1.0' #Using 1.0 since MongoMapper explicitly | |
%w(mongo mongo_mapper).each { | r | require r} | |
MongoMapper.connection = Mongo::Connection.new | |
MongoMapper.database = 'test' | |
class Widget | |
include MongoMapper::Document | |
key :code, String | |
key :description, String | |
key :price, Float | |
validates_numericality_of :price | |
validates_true_for :price, | |
:logic=> lambda { | x | (0.05..9999.99) === x.price }, | |
:message=>"must be in the following range: 0.05 to 9999.99" | |
end | |
puts "Create our first widget ..." | |
@w1=Widget.new(:code => 'A101', :description => '1/2 in. brass nut', :price => 0.15) | |
@w1.save | |
puts @w1.inspect | |
puts "We saved #{Widget.all.size} widgets so far ..." | |
puts "Create an invalid second widget" | |
@w2=Widget.new(:code => 'B101', :description => '1/2 in. brass bolt', :price => 0) | |
@w2.save | |
puts "Could not save this widget due to the following errors:" | |
puts @w2.errors.inspect | |
puts "Fix the price and re-save" | |
@w2.price = 0.2 | |
@w2.save | |
puts @w2.inspect | |
puts "We now saved #{Widget.all.size} widgets so far ..." | |
puts "Query widgets costing less than $0.2 ..." | |
@ws = Widget.all :price.lt => 0.2 | |
puts @ws.inspect | |
puts "Found #{@ws.size}" | |
#Enjoy! | |
# - Philippe(@techarch on Twitter) - http://blog.monnet-usa.com/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment