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.24 lib/mutant/loader.rb
mutant-0.10.23 lib/mutant/loader.rb
mutant-0.10.22 lib/mutant/loader.rb
mutant-0.10.21 lib/mutant/loader.rb
mutant-0.10.20 lib/mutant/loader.rb
mutant-0.10.19 lib/mutant/loader.rb
mutant-0.10.18 lib/mutant/loader.rb
mutant-0.10.17 lib/mutant/loader.rb
mutant-0.10.16 lib/mutant/loader.rb
mutant-0.10.15 lib/mutant/loader.rb
mutant-0.10.14 lib/mutant/loader.rb
mutant-0.10.13 lib/mutant/loader.rb
mutant-0.10.12 lib/mutant/loader.rb
mutant-0.10.11 lib/mutant/loader.rb
mutant-0.10.10 lib/mutant/loader.rb
mutant-0.10.9 lib/mutant/loader.rb
mutant-0.10.8 lib/mutant/loader.rb
mutant-0.10.7 lib/mutant/loader.rb
mutant-0.10.6 lib/mutant/loader.rb
mutant-0.10.5 lib/mutant/loader.rb