summaryrefslogtreecommitdiff
path: root/controllers/interfaces/eval.rb
blob: 6cb12b41dbd88eb4e38d36e4bb9b2cfef34a3376 (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
34
35
36
37
# frozen_string_literal: true

require 'pry'
require 'rainbow'

require_relative '../../logic/helpers/hash'

module NanoBot
  module Controllers
    module Interfaces
      module Eval
        def self.evaluate(input, cartridge, session)
          prefix = build_prefix(cartridge)
          postfix = build_postfix(cartridge)

          session.print(prefix) unless prefix.nil?

          session.evaluate_and_print(input, mode: 'eval')

          session.print(postfix) unless postfix.nil?
        end

        def self.build_prefix(cartridge)
          eval_interface =  Logic::Helpers::Hash.fetch(cartridge, %i[interfaces eval])
          return nil if eval_interface.nil?
          eval_interface[:prefix]
        end

        def self.build_postfix(cartridge)
          eval_interface =  Logic::Helpers::Hash.fetch(cartridge, %i[interfaces eval])
          return "\n" if eval_interface.nil? || !eval_interface.key?(:postfix) # default
          eval_interface[:postfix]
        end
      end
    end
  end
end