Guest User

Untitled

a guest
May 27th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. require 'dm-core'
  2.  
  3. DataMapper.setup :default,
  4. :adapter => 'postgres',
  5. :host => 'localhost',
  6. :port => 5432,
  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. end
  22.  
  23. class Dosage
  24. include DataMapper::Resource
  25.  
  26. property :id, Serial
  27. property :quantity, Integer
  28. belongs_to :item
  29. belongs_to :recipe
  30. end
  31.  
  32. class Recipe
  33. include DataMapper::Resource
  34.  
  35. property :id, Serial
  36. has 1, :product, :class_name => 'Item'
  37. has n, :dosages
  38. has n, :ingredients, :class_name => 'Item', :through => :dosages
  39. end
  40.  
  41. DataMapper.auto_migrate!
  42.  
  43. r = Recipe.new
  44. r.product = Item.first_or_create(:name => 'Bread')
  45. r.ingredients << [
  46. Dosage.new(:quantity => 2,
  47. :item => Item.first_or_create(:name => 'Flour')),
  48. Dosage.new(:quantity => 1,
  49. :item => Item.first_or_create(:name => 'Yeast')),
  50. Dosage.new(:quantity => 1,
  51. :item => Item.first_or_create(:name => 'Water'))
  52. ]
  53. r.save
Add Comment
Please, Sign In to add comment