Created
November 25, 2009 08:20
-
-
Save dkubb/242564 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 object | |
p Customer.all(customer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment