Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'dm-core'
- DataMapper.setup :default,
- :adapter => 'postgres',
- :host => 'localhost',
- :port => 5432,
- :username => 'dofus_treasury',
- :password => 'dofus_treasury',
- :database => 'dofus_treasury'
- # Items are identified by names, for example, Bread
- # A recipe has one product item, and a few ingredient items each dosed with a
- # quantity, for example, Bread = 2 Flour + 1 Yeast + 1 Water
- # A dosage is to used to hold one ingredient item and its quantity
- class Item
- include DataMapper::Resource
- property :name, String, :key => true
- has n, :dosages
- end
- class Dosage
- include DataMapper::Resource
- property :id, Serial
- property :quantity, Integer
- belongs_to :item
- belongs_to :recipe
- end
- class Recipe
- include DataMapper::Resource
- property :id, Serial
- has 1, :product, :class_name => 'Item'
- has n, :dosages
- has n, :ingredients, :class_name => 'Item', :through => :dosages
- end
- DataMapper.auto_migrate!
- r = Recipe.new
- r.product = Item.first_or_create(:name => 'Bread')
- r.ingredients << [
- Dosage.new(:quantity => 2,
- :item => Item.first_or_create(:name => 'Flour')),
- Dosage.new(:quantity => 1,
- :item => Item.first_or_create(:name => 'Yeast')),
- Dosage.new(:quantity => 1,
- :item => Item.first_or_create(:name => 'Water'))
- ]
- r.save
Add Comment
Please, Sign In to add comment