I've found myself starting to leverage checking persistence to get my models to 'work'. It seems convenient and correct to include a persistence check. On the other hand, it feels a little shady, as if I was being overly cautious or breaking the ORM abstraction in a small way.
An example might be:
class Shipment
include DataMapper:Resource
belongs_to :address, :required => false
def shippable?
valid? && persisted? && !address.nil? && address.valid? && address.persisted?
end
end
In this case, I need to have a method telling me if a shipment is shippable. This is true when its valid, saved to the db, and has an address saved.
Another example might be using it in callbacks to determine whether certain things (price recalculation) need to happen.
Opinions? Is it unnecessary, paranoid or safe, correct?