summaryrefslogtreecommitdiff
path: root/logic/cartridge/affixes.rb
blob: 0b40fa2bccd96c2fa4c7c6fcbec6cf7961e961cb (about) (plain)
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
# frozen_string_literal: true

require_relative '../helpers/hash'
require_relative './default'

module NanoBot
  module Logic
    module Cartridge
      module Affixes
        def self.get(cartridge, interface, direction, kind)
          affix = [
            {
              exists: (Helpers::Hash.fetch(cartridge, [:interfaces, direction]) || {}).key?(kind),
              value: Helpers::Hash.fetch(cartridge, [:interfaces, direction, kind])
            },
            {
              exists: (Helpers::Hash.fetch(cartridge, [:interfaces, interface, direction]) || {}).key?(kind),
              value: Helpers::Hash.fetch(cartridge, [:interfaces, interface, direction, kind])
            }
          ].filter { |candidate| candidate[:exists] }.last

          if affix.nil?
            return Helpers::Hash.fetch(
              Default.instance.values, [:interfaces, interface, direction, kind]
            )
          end

          affix[:value]
        end
      end
    end
  end
end