Use registration pattern for Payment kinds

Stephen Paul Weber created

Instead of a hard-coded case statement.

Change summary

lib/registration.rb | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)

Detailed changes

lib/registration.rb 🔗

@@ -105,20 +105,19 @@ class Registration
 	end
 
 	module Payment
+		def self.kinds
+			@kinds ||= {}
+		end
+
 		def self.for(iq, customer, tel)
-			case iq.form.field("activation_method")&.value&.to_s
-			when "bitcoin"
-				Bitcoin.new(iq, customer, tel)
-			when "credit_card"
-				CreditCard.for(iq, customer, tel)
-			when "code"
-				raise "TODO"
-			else
+			kinds.fetch(iq.form.field("activation_method")&.value&.to_s&.to_sym) {
 				raise "Invalid activation method"
-			end
+			}.call(iq, customer, tel)
 		end
 
 		class Bitcoin
+			Payment.kinds[:bitcoin] = method(:new)
+
 			def initialize(iq, customer, tel)
 				@reply = iq.reply
 				reply.note_type = :info
@@ -169,6 +168,8 @@ class Registration
 		end
 
 		class CreditCard
+			Payment.kinds[:credit_card] = ->(*args) { self.for(*args) }
+
 			def self.for(iq, customer, tel)
 				customer.payment_methods.then do |payment_methods|
 					if payment_methods.default_payment_method