Created
February 13, 2010 22:58
-
-
Save lsiden/303714 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
require 'rubygems' | |
require 'dm-core' | |
db_url = "sqlite3://#{File.dirname(__FILE__)}/example.db" | |
DataMapper.setup(:default, db_url) | |
class LedgerEntry | |
include DataMapper::Resource | |
property :id, Serial | |
property :description, String | |
property :amount, Float | |
end | |
class Ledger | |
include DataMapper::Resource | |
include ALC::DbObject | |
property :id, Serial | |
property :total, Float, :required => true, :default => 0 | |
has n, :entries, :model => 'LedgerEntry' | |
before :destroy do | |
entries.destroy if entries | |
end | |
end | |
class Option | |
include DataMapper::Resource | |
include ALC::DbObject | |
property :id, Serial | |
property :title, String | |
property :duration, Float # years until money runs out | |
has 1, :initial_costs, :model => 'Ledger' | |
has 1, :recurring_costs, :model => 'Ledger' | |
before :destroy do | |
initial_costs.destroy if initial_costs | |
recurring_costs.destroy if recurring_costs | |
end | |
end | |
class Account | |
include DataMapper::Resource | |
include ALC::DbObject | |
property :id, Serial | |
has 1, :assets, :model => 'Ledger' | |
has 1, :income, :model => 'Ledger' | |
has 1, :expenses, :model => 'Ledger' | |
has n, :options, :model => 'Option' | |
# Should never delete an account! | |
before :destroy do | |
throw halt | |
end | |
end | |
# instructs DataMapper to setup your database as needed | |
#DataMapper.auto_upgrade! | |
DataMapper.auto_migrate! |
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
require 'rubygems' | |
require 'spec' | |
require 'Model' | |
describe "ALC::Model::" do | |
it "Account.new should be able to create new account" do | |
account = Account.new | |
account.save.should be_true | |
end | |
it "Option.new should be able to create new option with account" do | |
account = Account.new | |
account.save.should be_true | |
option = Option.new ({ :title => "New Option", :account_id => account.id, }) | |
option.save.should be_true | |
end | |
it "Option.new should be able to create new ledger with Option " do | |
account = Account.new | |
account.save.should be_true | |
option = Option.new ({ :title => "New Option", :account_id => account.id}) | |
option.save.should be_true | |
ledger = Ledger.new( { :total => "3.3", :option_id => option.id}) | |
ledger.save.should be_true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment