freezolite 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bed25358ffb5313cf2816282bc8a2e38516a283d7373987808c17d476962a07
4
- data.tar.gz: 70c3a3a89ebbb11d3d8c81f40450124c0edfc20026e3deae1456ecde3bd09812
3
+ metadata.gz: 18bc2494d538e458f1ae9c25fd383df2d9b4c552b00d20680ada3c6ef0ad1b4b
4
+ data.tar.gz: b7adf7bff785d8a2968706f3229f60fdb2aefc59bfe5ce9c59aa165eb6f7410a
5
5
  SHA512:
6
- metadata.gz: c41594c8e976f6df02aa30854ac9dc3dab9b601fe73e648946704d3107b046dee1c2e10a072e1bed5d35b6782e783e45e23a4b43ee0de1606a3c549b8c99605b
7
- data.tar.gz: 8739d41c3cca71d36ff559de8e6ce5f25b51208af997ff2555ceaeb088bd47e2a2f404db7fe5f682e6c1699a7940d65a4d6faf205133072e842882a81be22ffa
6
+ metadata.gz: f1636d49db0a5fb812f1f95580ea4fec4742a96093e0bd14fa5b74567e15115190b1e0a23cb0d0666a639c8d01fcc16bcbf7f1a05b34fbc4e542fdf39a1cd535
7
+ data.tar.gz: 072207d396781631046c8bde02109528ecda9534475593950b7b18703344658efd560d79e931be1c9261f07111284d88f6a35089c35887b4cc1a55c416dff69c
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.5.0 (2024-04-29)
6
+
7
+ - Add constants freezing via the `# shareable_constant_value: ...` pragma. ([@palkan][])
8
+
9
+ ## 0.4.0 (2023-08-23)
10
+
11
+ - Upgrade to `require-hooks` 0.2.0. ([@palkan][])
12
+
5
13
  ## 0.3.0 (2023-07-19)
6
14
 
7
15
  - Ignore `./vendor` by default when using `freezolite/auto`. ([@palkan][])
data/README.md CHANGED
@@ -5,7 +5,11 @@
5
5
 
6
6
  Tired of adding `# frozen_string_literals: true` to every file in your project or running `rubocop -A` to make it do that for you? What if I told you that you can just add a single gem to your project and activate this option **for the project's files** automatically (without enabling it globally)?
7
7
 
8
- Freezolite is a gem that turn the `frozen_string_literal` compile option on only for the specified files. Thus, it's like running Ruby with `--enable=frozen-string-literal` but only for the files you own.
8
+ Freezolite is a gem that turns the `frozen_string_literal` compile option on only for the specified files. Thus it's like running Ruby with `--enable=frozen-string-literal` but only for the files you own.
9
+
10
+ > 📖 Read more about the motivation behind this project in ["Freezolite: the magic gem for keeping Ruby literals safely frozen"](https://evilmartians.com/chronicles/freezolite-the-magic-gem-for-keeping-ruby-literals-safely-frozen) post.
11
+
12
+ Freezolite also comes with experimental support for [freezing constants](#freezing-constants).
9
13
 
10
14
  ## Usage
11
15
 
@@ -15,7 +19,7 @@ Add the gem to your Gemfile:
15
19
  gem "freezolite"
16
20
  ```
17
21
 
18
- And drop the following line to your application entry-point **after loading dependencies** and before loading your application code. For example, in Rails, you can put it to `config/application.rb` after the `Bundler.require(...)` call:
22
+ And drop the following line into your application entry-point **after loading dependencies** and before loading your application code. For example, in Rails, you can put it in `config/application.rb` after the `Bundler.require(...)` call:
19
23
 
20
24
  ```ruby
21
25
  # config/application.rb
@@ -40,12 +44,30 @@ Freezolite.setup(
40
44
  )
41
45
  ```
42
46
 
43
- **NOTE**: When using auto mode, the `<project-root>/vendor/bundle` folder is excluded automatically. In manual mode, you may want to exclude it yourself.
47
+ > [!NOTE]
48
+ > When using auto mode, the `<project-root>/vendor/bundle` folder is excluded automatically. In manual mode, you may want to exclude it yourself.
44
49
 
45
50
  ### Using with Bootsnap
46
51
 
47
52
  Freezolite is compatible with Bootsnap. Just make sure you require it **after** Bootsnap. No manual cache invalidation required.
48
53
 
54
+ ### Freezing constants
55
+
56
+ Ruby 3 introduced a new pragma—[`# shareable_constant_value: ...`](https://bugs.ruby-lang.org/issues/17273). It's meant to be used with Ractor-driven applications to easily turn constants into Ractor-shareable objects within a source file. Making Ractor-shareable implies **deep freezing**, thus, we can leverage this pragma to automatically freeze constants!
57
+
58
+ Freezolite can automatically inject this pragma for you (only for matching files, similarly to frozen literals). You need to opt-in to enable this feature:
59
+
60
+ ```ruby
61
+ require "freezolite"
62
+ # You can also specify specific values, such as literal, experimental_copy, etc.
63
+ # Setting to `true` is equal to the `literal` value.
64
+ Freezolite.experimental_freeze_constants = true
65
+ require "freezolite/auto" # or setup manually
66
+ ```
67
+
68
+ > [!NOTE]
69
+ > This feature is considered experimental for the following reasons: 1) the pragma itself is not widely used and we still need to learn how to use it in our applications; 2) unlike `# frozen_string_literal`, this setting is not a compiler option but a parser context switch; thus, we have to use source transform here, and literally add a line to the source file, thus, breaking the line numbers in your stack traces, etc.
70
+
49
71
  ### Supported Ruby versions
50
72
 
51
73
  - Ruby (MRI) >= 2.7.0
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Freezolite # :nodoc:
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/freezolite.rb CHANGED
@@ -4,26 +4,32 @@ require "freezolite/version"
4
4
 
5
5
  module Freezolite
6
6
  class << self
7
- attr_accessor :patterns, :exclude_patterns
7
+ attr_reader :experimental_freeze_constants
8
8
 
9
- def setup(patterns:, exclude_patterns: nil)
10
- self.patterns = patterns
11
- self.exclude_patterns = exclude_patterns
9
+ def experimental_freeze_constants=(val)
10
+ @experimental_freeze_constants = (val == true) ? :literal : val
11
+ end
12
12
 
13
+ def setup(patterns:, exclude_patterns: nil)
13
14
  require "require-hooks/setup"
14
15
 
15
- ::RequireHooks.around_load do |path, &block|
16
- next block.call unless patterns.any? { |pattern| File.fnmatch?(pattern, path) }
17
- next block.call if exclude_patterns&.any? { |pattern| File.fnmatch?(pattern, path) }
16
+ ::RequireHooks.around_load(patterns: patterns, exclude_patterns: exclude_patterns) do |path, &block|
17
+ was_frozen_string_literal = ::RubyVM::InstructionSequence.compile_option[:frozen_string_literal]
18
+ ::RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true}
19
+ block.call
20
+ ensure
21
+ ::RubyVM::InstructionSequence.compile_option = {frozen_string_literal: was_frozen_string_literal}
22
+ end
18
23
 
19
- begin
20
- was_frozen_string_literal = ::RubyVM::InstructionSequence.compile_option[:frozen_string_literal]
21
- ::RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true}
22
- block.call
23
- ensure
24
- ::RubyVM::InstructionSequence.compile_option = {frozen_string_literal: was_frozen_string_literal}
24
+ if experimental_freeze_constants
25
+ val = experimental_freeze_constants
26
+ RequireHooks.source_transform(patterns: patterns, exclude_patterns: exclude_patterns) do |path, source|
27
+ source ||= File.read(path)
28
+ "# shareable_constant_value: #{val}\n#{source}"
25
29
  end
26
30
  end
27
31
  end
28
32
  end
33
+
34
+ self.experimental_freeze_constants = false
29
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freezolite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-19 00:00:00.000000000 Z
11
+ date: 2024-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: require-hooks
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.1'
19
+ version: '0.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.1'
26
+ version: '0.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -88,7 +88,7 @@ metadata:
88
88
  documentation_uri: https://github.com/ruby-next/freezolite
89
89
  homepage_uri: https://github.com/ruby-next/freezolite
90
90
  source_code_uri: https://github.com/ruby-next/freezolite
91
- post_install_message:
91
+ post_install_message:
92
92
  rdoc_options: []
93
93
  require_paths:
94
94
  - lib
@@ -103,8 +103,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
106
- rubygems_version: 3.4.8
107
- signing_key:
106
+ rubygems_version: 3.4.19
107
+ signing_key:
108
108
  specification_version: 4
109
109
  summary: Example description
110
110
  test_files: []