I’m wondering if there is a pattern for developing a method which requires several sequential steps?
As an example this, not especially great, example class makes a call to a remote service, if a bunch of conditions are met first.
Whilst not a giant if/else statement, it still has a smell about it, and the class is littered with exception messages all over the place.
I’d be very grateful for suggestions or examples of best practices for this sort of requirement
class RemoteResource
def self.post(product_id)
self.new.send(:build_response, product_id)
end
private
def build_response(product_id)
product_is_present?
not_already_sent?
valid?
post_to_remote_api
end
def product_is_present?(product_id)
product.present? || raise RuntimeError
end
def product
@product ||= Product.find(product_id)
end
def not_already_sent?
@product.sent_to_remote_at.nil? || raise RuntimeError
end
def valid?
# some additional validation method
true || raise RuntimeError
end
def post_to_remote_api
# send to some remote api
true
end
end