Sha256: 91831fe83584f9fdb70de3aa51edd51892236a7d65ed4d130727b2b07ad171d7

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true
module ProxyPacRb
  # JavaScript Runtimes
  module Runtimes
    RubyRacer = RubyRacerRuntime.new
    RubyRhino = RubyRhinoRuntime.new

    class << self
      def autodetect
        from_environment || best_available ||
          raise(RuntimeUnavailableError, 'Could not find a JavaScript runtime. ' \
                'See https://github.com/sstephenson/execjs for a list of available runtimes.')
      end

      def best_available
        runtimes.reject(&:deprecated?).find(&:available?)
      end

      def from_environment
        return nil unless ENV['JS_RUNTIME']

        runtime = const_get(ENV['JS_RUNTIME'])

        raise RuntimeUnavailableError, "#{ENV['JS_RUNTIME']} runtime is not defined" unless runtime
        raise RuntimeUnavailableError, "#{runtime.name} runtime is not available on this system" unless runtime.available?

        runtime
      end

      def names
        @names ||= constants.reduce({}) { |a, e| a.merge(const_get(e) => e) }.values
      end

      def runtimes
        @runtimes ||= [
          RubyRacer,
          RubyRhino
        ]
      end
    end

    def runtimes
      Runtimes.runtimes
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
proxy_pac_rb-2.1.0 lib/proxy_pac_rb/runtimes.rb
proxy_pac_rb-2.0.0 lib/proxy_pac_rb/runtimes.rb
proxy_pac_rb-1.0.0 lib/proxy_pac_rb/runtimes.rb