Sha256: 2d9cc0926c011196131aed20e4fc96571b836bcde3dda095051b97fe60e6e443

Contents?: true

Size: 1.13 KB

Versions: 7

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'sweet-moon'

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

        if params[:fennel] && params[:lua]
          raise StandardError, 'Adapter conflict: You can only use either Lua or Fennel, not both.'
        end

        if params[:fennel]
          content = fennel(content, params[:fennel])
        elsif params[:lua]
          content = lua(content, params[:lua])
        end

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

      def self.fennel(content, expression)
        path = "#{File.expand_path('../static/fennel', __dir__)}/?.lua"
        state = SweetMoon::State.new(package_path: path).fennel
        state.fennel.eval("(set _G.adapter (fn [content] #{expression}))")
        adapter = state.get(:adapter)
        adapter.call([content])
      end

      def self.lua(content, expression)
        state = SweetMoon::State.new
        state.eval("adapter = function(content) return #{expression}; end")
        adapter = state.get(:adapter)
        adapter.call([content])
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
nano-bots-0.1.0 components/adapter.rb
nano-bots-0.0.10 components/adapter.rb
nano-bots-0.0.9 components/adapter.rb
nano-bots-0.0.8 components/adapter.rb
nano-bots-0.0.7 components/adapter.rb
nano-bots-0.0.6 components/adapter.rb
nano-bots-0.0.5 components/adapter.rb