Guest User

Untitled

a guest
May 27th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. require 'dm-core'
  2.  
  3. DataMapper.setup :default,
  4. :adapter => 'postgres',
  5. :host => 'localhost',
  6. :port => 5433,
  7. :username => 'dofus_treasury',
  8. :password => 'dofus_treasury',
  9. :database => 'dofus_treasury'
  10.  
  11. # Items are identified by names, for example, Bread
  12. # A recipe has one product item, and a few ingredient items each dosed with a
  13. # quantity, for example, Bread = 2 Flour + 1 Yeast + 1 Water
  14. # A dosage is to used to hold one ingredient item and its quantity
  15.  
  16. class Item
  17. include DataMapper::Resource
  18.  
  19. property :name, String, :key => true
  20. has n, :dosages
  21. has n, :recipes, :through => :dosages
  22. end
  23.  
  24. class Dosage
  25. include DataMapper::Resource
  26.  
  27. property :id, Serial
  28. property :quantity, Integer
  29. belongs_to :item
  30. belongs_to :recipe
  31. end
  32.  
  33. class Recipe
  34. include DataMapper::Resource
  35.  
  36. property :id, Serial
  37. has 1, :product, :class_name => 'Item'
  38. has n, :dosages
  39. has n, :ingredients, :class_name => 'Item', :child_key => :item,
  40. :through => :dosages
  41. end
  42.  
  43. Recipe.new do |r|
  44. r.product = Item.new(:name => 'Bread')
  45. r.ingredients << Dosage.new do |d|
  46. d.quantity = 2
  47. d.item = Item.new(:name => 'Flour')
  48. end
  49. r.ingredients << Dosage.new do |d|
  50. d.quantity = 1
  51. d.item = Item.new(:name => 'Yeast')
  52. end
  53. r.ingredients << Dosage.new do |d|
  54. d.quantity = 1
  55. d.item = Item.new(:name => 'Water')
  56. end
  57. end.save
  58.  
  59. DataMapper.auto_migrate!
Add Comment
Please, Sign In to add comment