summaryrefslogtreecommitdiff
path: root/components/adapter.rb
blob: 32aa1694509e10b3985faf265f4829fbcff5a332 (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
# frozen_string_literal: true

require_relative 'embedding'
require_relative '../logic/cartridge/safety'

module NanoBot
  module Components
    class Adapter
      def self.apply(params, cartridge)
        content = params[:content]

        raise StandardError, 'conflicting adapters' if %i[fennel lua clojure].count { |key| !params[key].nil? } > 1

        call = {
          parameters: %w[content], values: [content],
          safety: { sandboxed: Logic::Cartridge::Safety.sandboxed?(cartridge) }
        }

        if params[:fennel]
          call[:source] = params[:fennel]
          content = Components::Embedding.fennel(**call)
        elsif params[:clojure]
          call[:source] = params[:clojure]
          content = Components::Embedding.clojure(**call)
        elsif params[:lua]
          call[:source] = params[:lua]
          content = Components::Embedding.lua(**call)
        end

        "#{params[:prefix]}#{content}#{params[:suffix]}"
      end
    end
  end
end