Sha256: ffac49c21c592ebe39249c15edf62f7da5459efa066c3d9a33b04c21e31f55e1

Contents?: true

Size: 1.23 KB

Versions: 38

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Mutant
  class Loader
    include Anima.new(:binding, :kernel, :source, :subject)

    FROZEN_STRING_FORMAT = "# frozen_string_literal: true\n%s"
    VOID_VALUE_REGEXP    = /\A[^:]+:\d+: void value expression/.freeze

    private_constant(*constants(false))

    class Result
      include Singleton

      # Vale returned on successful load
      class Success < self
      end # Success

      # Vale returned on MRI detecting void value expressions
      class VoidValue < self
      end # voidValue
    end # Result

    # Call loader
    #
    # @return [Result]
    def self.call(*arguments)
      new(*arguments).call
    end

    # Call loader
    #
    # One off the very few valid uses of eval ever.
    #
    # @return [Result]
    #
    # rubocop:disable Metrics/MethodLength
    def call
      kernel.eval(
        FROZEN_STRING_FORMAT % source,
        binding,
        subject.source_path.to_s,
        subject.source_line
      )
    rescue SyntaxError => exception
      # rubocop:disable Style/GuardClause
      if VOID_VALUE_REGEXP.match?(exception.message)
        Result::VoidValue.instance
      else
        raise
      end
    else
      Result::Success.instance
    end
  end # Loader
end # Mutant

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
mutant-0.10.4 lib/mutant/loader.rb
mutant-0.10.1 lib/mutant/loader.rb
mutant-0.10.0 lib/mutant/loader.rb
mutant-0.9.14 lib/mutant/loader.rb
mutant-0.9.13 lib/mutant/loader.rb
mutant-0.9.12 lib/mutant/loader.rb
mutant-0.9.11 lib/mutant/loader.rb
mutant-0.9.10 lib/mutant/loader.rb
mutant-0.9.9 lib/mutant/loader.rb
mutant-0.9.8 lib/mutant/loader.rb
mutant-0.9.7 lib/mutant/loader.rb
mutant-0.9.6 lib/mutant/loader.rb
mutant-0.9.5 lib/mutant/loader.rb
mutant-0.9.4 lib/mutant/loader.rb
mutant-0.9.3 lib/mutant/loader.rb
mutant-0.9.2 lib/mutant/loader.rb
mutant-0.9.1 lib/mutant/loader.rb
mutant-0.9.0 lib/mutant/loader.rb