Triggers

You can define triggers as methods by defining pre_commit_trigger and post_commit_trigger, which run before or after the commit, respectively.

class Payment < Lafcadio::DomainObject
  domain_object User
  float         'amount'

  def post_commit_trigger
    self.user.email_thanks_for_your_payment_letter
  end
end

You can use triggers to check values and raise exceptions, if you like.

class Payment < Lafcadio::DomainObject
  domain_object User
  float         'amount'

  def pre_commit_trigger
    raise if amount < 0
  end
end

Each domain object automatically maintains a hash of values called @original_values, which records the last values known to come from the database. So if the domain object has been withdrawn from the database, @original_values will contain the values from the database regardless of changes made to the Ruby version of the domain object. When you commit to the database, @original_values is reset.

class Payment < Lafcadio::DomainObject
  domain_object User
  float         'amount'

  def pre_commit_trigger
    raise if amount != @original_values['amount']
  end
end