Refactor command list to use composition

Stephen Paul Weber created

Also rename buy credit to top up

Change summary

lib/command_list.rb       | 53 +++++++++++++++++++++-------------------
sgx_jmp.rb                |  2 
test/test_command_list.rb | 16 ++++++------
3 files changed, 37 insertions(+), 34 deletions(-)

Detailed changes

lib/command_list.rb 🔗

@@ -22,40 +22,43 @@ class CommandList
 				REDIS.get("catapult_fwd-#{tel}"),
 				customer.plan_name ? customer.payment_methods : []
 			]).then do |(fwd, payment_methods)|
-				klass = Class.new(Registered)
-				klass.include(HasBilling) unless payment_methods.empty?
-				klass.include(HasForwarding) if fwd
-				klass.new
+				Registered.new(*[
+					(HAS_CREDIT_CARD unless payment_methods.empty?),
+					(HAS_FORWARDING if fwd)
+				].compact)
 			end
 		end
 
-		def each
-			super
-			yield node: "number-display", name: "Display JMP Number"
-			yield node: "configure-calls", name: "Configure Calls"
-			yield node: "usage", name: "Show Monthly Usage"
-			yield node: "reset sip account", name: "Create or Reset SIP Account"
-			yield(
+		def initialize(*args)
+			@extra = args
+		end
+
+		ALWAYS = [
+			{ node: "number-display", name: "Display JMP Number" },
+			{ node: "configure-calls", name: "Configure Calls" },
+			{ node: "usage", name: "Show Monthly Usage" },
+			{ node: "reset sip account", name: "Create or Reset SIP Account" },
+			{
 				node: "credit cards",
 				name: "Credit Card Settings and Management"
-			)
-		end
-	end
+			}
+		].freeze
 
-	module HasForwarding
 		def each
 			super
-			yield(
-				node: "record-voicemail-greeting",
-				name: "Record Voicemail Greeting"
-			)
+			([ALWAYS] + @extra).each do |commands|
+				commands.each { |x| yield x }
+			end
 		end
 	end
 
-	module HasBilling
-		def each
-			super
-			yield node: "buy credit", name: "Buy account credit"
-		end
-	end
+
+	HAS_FORWARDING = [
+		node: "record-voicemail-greeting",
+		name: "Record Voicemail Greeting"
+	].freeze
+
+	HAS_CREDIT_CARD = [
+		node: "top up", name: "Buy Account Credit by Credit Card"
+	].freeze
 end

sgx_jmp.rb 🔗

@@ -344,7 +344,7 @@ command :execute?, node: "credit cards", sessionid: nil do |iq|
 	}.catch { |e| panic(e, sentry_hub) }
 end
 
-command :execute?, node: "buy credit", sessionid: nil do |iq|
+command :execute?, node: "top up", sessionid: nil do |iq|
 	sentry_hub = new_sentry_hub(iq, name: iq.node)
 	reply = iq.reply
 	reply.allowed_actions = [:complete]

test/test_command_list.rb 🔗

@@ -63,14 +63,14 @@ class CommandListTest < Minitest::Test
 			)),
 			["registered"]
 		)
-		assert_kind_of(
-			CommandList::HasForwarding,
-			CommandList.for("registered").sync
+		assert_equal(
+			CommandList::HAS_FORWARDING,
+			CommandList::HAS_FORWARDING & CommandList.for("registered").sync.to_a
 		)
 	end
 	em :test_for_registered_with_fwd
 
-	def test_for_registered_with_billing
+	def test_for_registered_with_credit_card
 		CommandList::REDIS.expect(
 			:get,
 			EMPromise.resolve(nil),
@@ -85,12 +85,12 @@ class CommandListTest < Minitest::Test
 			)),
 			["registered"]
 		)
-		assert_kind_of(
-			CommandList::HasBilling,
-			CommandList.for("registered").sync
+		assert_equal(
+			CommandList::HAS_CREDIT_CARD,
+			CommandList::HAS_CREDIT_CARD & CommandList.for("registered").sync.to_a
 		)
 	end
-	em :test_for_registered_with_billing
+	em :test_for_registered_with_credit_card
 
 	def test_for_registered_with_forwarding_and_billing
 		CommandList::REDIS.expect(