Skip to content

Commit

Permalink
Added UTA::Models::Transaction.
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyvoase committed Dec 30, 2010
1 parent c1a49dc commit 3e61d1b
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions lib/uta/models/transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'uuid'

module UTA::Models
# An instance of <http://uptheasset.org/ontology#Transaction>.
#
# A transaction is the record of an exchange between two or more accounts in
# a journal. Every transaction should have at least two entries (one credit,
# one debit).
class Transaction
include Spira::Resource

base_uri "this:transactions#"
type RDF::UTA.Transaction

property :id, :predicate => RDF::DC.identifier
property :date, :predicate => RDF::DC.date
property :label, :predicate => RDF::RDFS.label
property :comment, :predicate => RDF::RDFS.comment

has_many :entries, :predicate => RDF::UTA.entry, :type => :Entry

# Generate and save a new transaction (with its own UUID).
#
# @param [Hash] attributes
# An optional hash of attributes to set on the generated transaction.
#
# @yield [Transaction] The generated transaction (not yet saved).
#
# @return [Transaction] The saved transaction.
#
# @example Record a sale
# Transaction.generate do |t|
# t.credit 30, RDF::URI.new("accounts#revenue")
# t.debit 30, RDF::URI.new("accounts#cash")
# end
def self.generate(attributes = {})
uuid = ::UUID.new.generate
transaction = self.for(uuid)
transaction.id = RDF::UUID[uuid]
transaction.date = Date.today
transaction.update(attributes)
yield transaction if block_given?
transaction.save!
end

# Add a (saved) credit {Entry} to this transaction.
#
# @param [RDF::Value] amount
# The amount by which to credit.
# @param [Account, RDF::URI] account
# The account to credit.
# @return [Entry] the created entry.
def credit(amount, account)
self.entries << Entry.credit(amount, account).save!
end

# Add a (saved) debit {Entry} to this transaction.
#
# @param [RDF::Value] amount
# The amount by which to debit.
# @param [Account, RDF::URI] account
# The account to debit.
# @return [Entry] the created entry.
def debit(amount, account)
self.entries << Entry.debit(amount, account).save!
end
end
end

0 comments on commit 3e61d1b

Please sign in to comment.