CustomerPlan.extract

Stephen Paul Weber created

To allow building from a hash with extra keys. Makes the CustomerRepo more readable.

Change summary

lib/customer_plan.rb | 10 ++++++++++
lib/customer_repo.rb | 13 ++++++-------
2 files changed, 16 insertions(+), 7 deletions(-)

Detailed changes

lib/customer_plan.rb 🔗

@@ -18,6 +18,16 @@ class CustomerPlan
 		new(customer_id, plan: plan_name&.then(&Plan.method(:for)), **kwargs)
 	end
 
+	def self.extract(customer_id, **kwargs)
+		self.for(
+			customer_id,
+			**kwargs.slice(
+				:plan_name, :expires_at,
+				:auto_top_up_amount, :monthly_overage_limit
+			)
+		)
+	end
+
 	def initialize(
 		customer_id,
 		plan: nil,

lib/customer_repo.rb 🔗

@@ -152,10 +152,10 @@ protected
 
 	def fetch_all(customer_id)
 		EMPromise.all([
-			@sgx_repo.get(customer_id),
+			@sgx_repo.get(customer_id).then { |sgx| { sgx: sgx } },
 			@db.query_one(SQL, customer_id, default: {}),
 			fetch_redis(customer_id)
-		]).then { |sgx, sql, redis| [sgx, sql.merge(redis)] }
+		]).then { |all| all.reduce(&:merge) }
 	end
 
 	def tndetails(sgx)
@@ -166,13 +166,12 @@ protected
 
 	def find_inner(customer_id, jid)
 		set_user.call(customer_id: customer_id, jid: jid)
-		fetch_all(customer_id).then do |(sgx, data)|
+		fetch_all(customer_id).then do |data|
 			Customer.new(
 				customer_id, Blather::JID.new(jid),
-				sgx: sgx, tndetails: tndetails(sgx), plan: CustomerPlan.for(
-					customer_id,
-					**data.reject { |(k, _)| k == :balance }
-				), **data.slice(:balance)
+				tndetails: tndetails(data[:sgx]),
+				plan: CustomerPlan.extract(customer_id, data),
+				**data.slice(:balance, :sgx, :tndetails)
 			)
 		end
 	end