~singpolyma/sgx-jmp

ref: 9a1a09ee69626d82266b85be09cb41f868e02e87 sgx-jmp/lib/command_list.rb -rw-r--r-- 876 bytes
9a1a09eeStephen Paul Weber Refactor commands to have Command and Command::Execution objects 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

class CommandList
	include Enumerable

	def self.register(command)
		@commands ||= []
		@commands << command
	end

	def self.for(customer)
		EMPromise.resolve(customer&.registered?).catch { nil }.then do |reg|
			args_for(customer, reg).then do |kwargs|
				new(@commands.select { |c| c.list_for?(**kwargs) })
			end
		end
	end

	def self.args_for(customer, reg)
		args = { customer: customer, tel: reg ? reg.phone : nil }
		return EMPromise.resolve(args) unless args[:tel]

		EMPromise.all([
			REDIS.get("catapult_fwd-#{args[:tel]}"),
			customer.plan_name ? customer.payment_methods : []
		]).then do |(fwd, payment_methods)|
			args.merge(fwd: fwd, payment_methods: payment_methods)
		end
	end

	def initialize(commands)
		@commands = commands
	end

	def each(&blk)
		@commands.map { |c| { node: c.node, name: c.name } }.each(&blk)
	end
end