Skip to content

Instantly share code, notes, and snippets.

@dsisnero
Forked from dkubb/gist:242564
Created May 12, 2010 20:33
Show Gist options
  • Save dsisnero/399088 to your computer and use it in GitHub Desktop.
Save dsisnero/399088 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -Ku
# encoding: utf-8
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
class Customer
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true, :length => 1..100
has n, :orders
has n, :items, :through => :orders
end
class Order
include DataMapper::Resource
property :id, Serial
property :reference, String, :required => true, :length => 1..20
belongs_to :customer
has n, :order_lines
has n, :items, :through => :order_lines
end
class OrderLine
include DataMapper::Resource
property :id, Serial
property :quantity, Integer, :required => true, :default => 1, :min => 1
property :unit_price, Decimal, :required => true, :default => lambda { |r,p| r.item.unit_price }
belongs_to :order
belongs_to :item
end
class Item
include DataMapper::Resource
property :id, Serial
property :sku, String, :required => true, :length => 1..20
property :unit_price, Decimal, :required => true, :min => 0
has n, :order_lines
end
DataMapper.auto_migrate!
puts '-' * 80
customer = {
:name => 'Dan Kubb',
:orders => [
{
:reference => 'TEST1234',
:order_lines => [
{
:item => {
:sku => 'BLUEWIDGET1',
:unit_price => 1.00,
},
},
],
},
]
}
# create the Customer with a nested options
Customer.create(customer)
puts '-' * 80
# the options to create can be used to retrieve the same obejct
p Customer.all(customer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment