Skip to content

Instantly share code, notes, and snippets.

@sajoku
Created December 17, 2014 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajoku/a2b90bcc8dea8fe69262 to your computer and use it in GitHub Desktop.
Save sajoku/a2b90bcc8dea8fe69262 to your computer and use it in GitHub Desktop.
This is a sample order_syncer which syncs externals and needs the output of what is being synced in the next method.
class OrderSyncer
def initialize(order)
@order = order
end
def synchronise_all
external_order_id = synchronise_order(@order)
external_payment_id = synchronise_payment(external_order_id)
external_contract_id = synchronise_contract(external_payment_id)
synchronise_authorisisation(external_contract_id)
end
private
def synchronise_order(order)
# If the fetcher does not return an order_id we need to synchronise
unless order_id = Fetcher::OrderId.new(order.id).fetch
order_id = Synchroniser::Order.new(order).synchronise
end
end
def synchronise_payment(external_order_id)
#Same kind of steps as in synchronise order
end
def synchronise_contract(external_payment_id)
#Same kind of steps as in synchronise order
end
def synchronise_authorisisation(external_contract_id)
#last step which always synchronises and returns an id or nil
end
end
@sajoku
Copy link
Author

sajoku commented Dec 17, 2014

The problems with this setup (imo):

  1. The synchronise_all is getting really big for 12 steps.
  2. The separate synchronise methods depend on each other being called in succession. (Don't know if I see this as a problem or just something that I should accept).
  3. The OrderSynchroniser as it stands is 150 lines. Not a problem per se but thinking about changing behaviour in this class is a pain.

Solutions I thought of but fear will overcomplicate the actual code:

  1. Extract the separate synchronise methods to their own classes. So the synchronise_order method will be extract to the class SynchroniseOrder which will Fetch OR Synchronise the order. Although this will make the OrderSynchroniser class smaller now I have 3 separate classes that are being called.
  2. Since the synchronise_methods are all alike I thought about creating a more generic method which will be called with the classes it needs to call, making the setup some what more abstract and generic. I'm not sure this would be a step in the right direction since I'm afraid that it will make the code to abstract and hard to change in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment