Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save knzconnor/878640 to your computer and use it in GitHub Desktop.
Save knzconnor/878640 to your computer and use it in GitHub Desktop.
A pseudo-OODB style of using MongoMapper's plugin system to create composed objects.
#rvm use 1.9.2
#brew install mongo
#gem install mongo_mapper
#gem install bson_ext
require 'rubygems'
require 'mongo_mapper'
MongoMapper.database = 'mm_demo'
module MMDemo
module HasDoorsAndWindows
def self.configure(model)
model.key :doors, Integer, :default => 0
model.key :windows, Integer, :default => 0
model.key :sun_roof, Boolean, :default => false
end
module InstanceMethods
def enterable?
doors > 0
end
def well_lit?
windows >= (doors * 5)
end
end
end
end
class House
include MongoMapper::Document
plugin MMDemo::HasDoorsAndWindows
end
class Vehicle
include MongoMapper::Document
key :wheels, Integer
end
class Motorcyle < Vehicle
end
class Car < Vehicle
plugin MMDemo::HasDoorsAndWindows
def well_lit?
sunroof?
end
end
require 'minitest/autorun'
describe "a House" do
it "has doors" do
House.new(:doors => 2).doors.must_equal 2
end
it "has windows" do
House.new(:doors => 7).doors.must_equal 7
end
it "is enterable if there are any doors" do
House.new(:doors => 0).enterable?.must_equal false
House.new(:doors => 4).enterable?.must_equal true
end
it "is enterable if there are any doors" do
House.new(:doors => 0).enterable?.must_equal false
House.new(:doors => 4).enterable?.must_equal true
end
it "is well lit if there are 5x as many windows as doors" do
House.new(:doors => 1, :windows => 5).well_lit?.must_equal true
House.new(:doors => 1, :windows => 4).well_lit?.must_equal false
end
end
describe "a Car" do
it "is well lit if it has a sunroof" do
Car.new(:sunroof => true).well_lit?.must_equal true
Car.new(:sunroof => false).well_lit?.must_equal false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment