synvert-core 1.33.3 → 1.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0b171e345173ce6e1519cc68c1fd4aca91e796d56554ba63875c0402b3b202f
4
- data.tar.gz: 218dcbcacb00ff39d4de981dc76178c552081577fe4cfaeadad291a1b42b7d46
3
+ metadata.gz: 7407325ab10af6e8e90b2a2e10bc737686a555603f362319779a2a9868d8ba71
4
+ data.tar.gz: eb2f33a8ea48fc4836af3529ca4f684c7c8e1c98a797d55703ada5b60505878e
5
5
  SHA512:
6
- metadata.gz: 6c96410cb92044bfeca2ebbc07cd2340973bd3320a10060c4bdad4a95db48d31d62e358a9929c40639904633dd9cd09238d34b3ce877b51d2af1a7a920cb8747
7
- data.tar.gz: '0702947b02c043b28fc52456497e03834e94407514ccc34ae9be9f562a885cffbd9f08f0e598fe0df6d03e9c36bc4f521c1169079de748c0fadc082f76865bac'
6
+ metadata.gz: 70c762d52252e31200861d30d418f4f634ff4f9dfcacd1d11964865be664118a78a6a76362614232f5a1b465ad8862d974adfa1f3baed6395cac6dac000ebffc
7
+ data.tar.gz: df130135e5df9453b3f4d6847c68aaf0703fb31765b384ca86c31ca52c0d60c60f64717ac2b26457d872a5a523a640cb5f6a9b7715fefe2b3df2008f691e3b01
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.34.0 (2024-02-29)
4
+
5
+ * Add `with_configurations` dsl
6
+ * Add `save_data` dsl
7
+ * Add `load_data` dsl
8
+
3
9
  ## 1.33.3 (2024-02-28)
4
10
 
5
11
  * Preserve current parser
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- synvert-core (1.33.3)
4
+ synvert-core (1.34.0)
5
5
  activesupport (< 7.0.0)
6
6
  node_mutation (>= 1.23.2)
7
7
  node_query (>= 1.15.1)
data/README.md CHANGED
@@ -70,6 +70,9 @@ DSLs are as follows
70
70
  * [helper_method](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#helper_method-instance_method) - define a helper method
71
71
  * [add_snippet](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#add_snippet-instance_method) - call another rewriter
72
72
  * [call_helper](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#call_helper-instance_method) - call a shared rewriter
73
+ * [with_configurations](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#with_configurations-instance_method) - execute a block of code with temporary configurations
74
+ * [save_data](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#save_data-instance_method) - save data
75
+ * [load_data](https://xinminlabs.github.io/synvert-core-ruby/Synvert/Core/Rewriter.html#load_data-instance_method) - load data
73
76
 
74
77
  Scopes:
75
78
 
@@ -100,6 +100,32 @@ module Synvert::Core
100
100
  def test_result
101
101
  @test_result || 'actions'
102
102
  end
103
+
104
+ # Temporarily sets the specified configurations, executes the given block, and then restores the original configurations.
105
+ #
106
+ # @param configurations [Hash] The configurations to be set temporarily.
107
+ # @yield The block of code to be executed.
108
+ #
109
+ # @example
110
+ # with_temporary_configurations({ number_of_workers: 1 }) do
111
+ # # Code to be executed with temporary configurations
112
+ # end
113
+ def with_temporary_configurations(configurations, &block)
114
+ old_instance_variables = instance_variables.reduce({}) do |hash, var|
115
+ hash[var] = instance_variable_get(var)
116
+ hash
117
+ end
118
+
119
+ configurations.each do |variable, value|
120
+ instance_variable_set("@#{variable}", value)
121
+ end
122
+
123
+ block.call
124
+
125
+ old_instance_variables.each do |var, value|
126
+ instance_variable_set(var, value)
127
+ end
128
+ end
103
129
  end
104
130
  end
105
131
  end
@@ -409,6 +409,30 @@ module Synvert::Core
409
409
  @helpers << { name: name, block: block }
410
410
  end
411
411
 
412
+ # Executes a block of code with temporary configurations.
413
+ #
414
+ # @param configurations [Hash] The temporary configurations to apply.
415
+ # @yield The block of code to execute.
416
+ def with_configurations(configurations, &block)
417
+ Configuration.with_temporary_configurations(configurations, &block)
418
+ end
419
+
420
+ # Saves data with a given key and value.
421
+ #
422
+ # @param key [Symbol] the key to identify the data
423
+ # @param value [Object] the value to be saved
424
+ def save_data(key, value)
425
+ Synvert::Core.instance_variable_set("@#{key}", value)
426
+ end
427
+
428
+ # Loads data with the given key.
429
+ #
430
+ # @param key [Symbol] the key of the data to be loaded
431
+ # @return [Object] the data loaded
432
+ def load_data(key)
433
+ Synvert::Core.instance_variable_get("@#{key}")
434
+ end
435
+
412
436
  private
413
437
 
414
438
  # Handle one file.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Synvert
4
4
  module Core
5
- VERSION = '1.33.3'
5
+ VERSION = '1.34.0'
6
6
  end
7
7
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ module Synvert::Core
4
+ RSpec.describe Configuration do
5
+ after do
6
+ Configuration.number_of_workers = nil
7
+ Configuration.strict = nil
8
+ end
9
+
10
+ describe '.with_temporary_configurations' do
11
+ it 'temporarily sets instance variables and restores them after block execution' do
12
+ Configuration.number_of_workers = 4
13
+ Configuration.strict = true
14
+
15
+ Configuration.with_temporary_configurations(number_of_workers: 1, strict: false) do
16
+ expect(Configuration.number_of_workers).to eq(1)
17
+ expect(Configuration.strict).to eq(false)
18
+ end
19
+
20
+ expect(Configuration.number_of_workers).to eq(4)
21
+ expect(Configuration.strict).to eq(true)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -395,6 +395,18 @@ module Synvert::Core
395
395
  expect(instance.dynamic_helper('arg1', 'arg2')).to eq 'dynamic result'
396
396
  end
397
397
 
398
+ it 'parses save_data and load_data' do
399
+ data = nil
400
+ rewriter =
401
+ Rewriter.new 'group', 'name' do
402
+ hash = { foo: 'bar' }
403
+ save_data :object, hash
404
+ data = load_data :object
405
+ end
406
+ rewriter.process
407
+ expect(data).to eq({ foo: 'bar' })
408
+ end
409
+
398
410
  describe 'class methods' do
399
411
  before :each do
400
412
  Rewriter.clear
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.33.3
4
+ version: 1.34.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-28 00:00:00.000000000 Z
11
+ date: 2024-02-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -196,6 +196,7 @@ files:
196
196
  - lib/synvert/core/version.rb
197
197
  - spec/spec_helper.rb
198
198
  - spec/support/parser_helper.rb
199
+ - spec/synvert/core/configuration_spec.rb
199
200
  - spec/synvert/core/engine/erb_spec.rb
200
201
  - spec/synvert/core/engine/haml_spec.rb
201
202
  - spec/synvert/core/engine/slim_spec.rb
@@ -242,6 +243,7 @@ summary: convert ruby code to better syntax.
242
243
  test_files:
243
244
  - spec/spec_helper.rb
244
245
  - spec/support/parser_helper.rb
246
+ - spec/synvert/core/configuration_spec.rb
245
247
  - spec/synvert/core/engine/erb_spec.rb
246
248
  - spec/synvert/core/engine/haml_spec.rb
247
249
  - spec/synvert/core/engine/slim_spec.rb