Use for spawning multiple objects
class Foo
attr_writer :bar_source
private
def bar_source
@bar_source ||= Bar.public_method(:new)
end
end
For a has-one relationship
class Secretary
def create_appointment(doctor, patient, appointment=nil)
appointment ||= Appointment.new
appointment.tap do |appointment|
appointment.doctor = doctor
appointment.patient = patient
end
end
end
This is a little weird. Check if your class isn't doing too little or if you shouldn't rename things.
For when there's a complete creation method
class Foo; end
before do
Foo.stub(:for_bar).and_return(qux)
end
class Foo
def initialize
@qux =