From 1448fc7df829cd2deb3190d4e3ebe477b4f8a20f Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 5 May 2021 09:29:04 -0500 Subject: [PATCH] Use registration pattern for Payment kinds Instead of a hard-coded case statement. --- lib/registration.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/registration.rb b/lib/registration.rb index da1611aa2a6bafef2729d8ac6aa3972bd7c00def..9380b6f55c81ac1fa80f8f6b57bb3626d33f9a4b 100644 --- a/lib/registration.rb +++ b/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