Module: Freezolite

Defined in:
lib/freezolite/version.rb,
lib/freezolite.rb

Overview

:nodoc:

Constant Summary collapse

VERSION =
"0.7.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.experimental_freeze_constantsObject

Returns the value of attribute experimental_freeze_constants.



7
8
9
# File 'lib/freezolite.rb', line 7

def experimental_freeze_constants
  @experimental_freeze_constants
end

Class Method Details

.setup(patterns:, exclude_patterns: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/freezolite.rb', line 13

def setup(patterns:, exclude_patterns: nil)
  require "require-hooks/setup"

  ::RequireHooks.around_load(patterns: patterns, exclude_patterns: exclude_patterns) do |path, &block|
    was_frozen_string_literal = ::RubyVM::InstructionSequence.compile_option[:frozen_string_literal] || false
    ::RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true}
    block.call
  ensure
    ::RubyVM::InstructionSequence.compile_option = {frozen_string_literal: was_frozen_string_literal}
  end

  default_frozen_string_literal = ::RubyVM::InstructionSequence.compile_option[:frozen_string_literal] || false

  ::RequireHooks.around_load do |path, &block|
    ::RubyVM::InstructionSequence.compile_option = {frozen_string_literal: default_frozen_string_literal}
    block.call
  end

  if experimental_freeze_constants
    val = experimental_freeze_constants
    RequireHooks.source_transform(patterns: patterns, exclude_patterns: exclude_patterns) do |path, source|
      source ||= File.read(path)
      "# shareable_constant_value: #{val}\n#{source}"
    end
  end
end