patchyaml 0.1.3 → 0.1.4

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: 55842aa3173a34dd262ddd288264ca567e7f1952ff48b45173b995de12dddd9f
4
- data.tar.gz: d08a821c86738f7f444bbf98ed9dc04bb2cdb915c03a6da46173b73d14929baa
3
+ metadata.gz: 27d24b69a097c75a45afb302b0b4199b9bc2ed45bd34d8cf141717def23e6f03
4
+ data.tar.gz: ec42ec73c55a184ee211cc1b9fbc3f907380d85fa2b5fc951bdaeb2abde66e69
5
5
  SHA512:
6
- metadata.gz: 70f569d41f4117778542b37cd4bb19012cee815b43e6997e011c73738ef68b038f6436595bc7eb0ad04b5a188b1ad51180ad6f1e9b2b9dae8612bbd9471c56e2
7
- data.tar.gz: 38645753c1638a167ed83960b1f8a59bde245ef07c785378a990cc4cf4d1d2c08cb8f697aa561ae8800de544937bc67b847a7f0958c28234a558cfe87971c1c7
6
+ metadata.gz: 579bec7e6859c1befd69cebcbf7e67d1ab7c4352bfd377160838fbd89d22d98602d251e64772d26d1428fa9e801202cd881ed7e2d35625b5e499a7498328c3f2
7
+ data.tar.gz: c0e4cdc1e179f6832663a102585e838a79f7083f8c7f1abad058685214ef5af8c2b3f1531acaf40543aeb8cdf6fe3fa77b856e0b1a957f087aafdc3ad44ea700
@@ -15,7 +15,7 @@ module PatchYAML
15
15
  #
16
16
  # Returns true if the path exists, false otherwise.
17
17
  def exist?(path)
18
- value, parent = find(@stream, path)
18
+ value, = find(@stream, path)
19
19
  !value.nil?
20
20
  end
21
21
 
@@ -73,7 +73,7 @@ module PatchYAML
73
73
  private
74
74
 
75
75
  def reload(data)
76
- @data = data
76
+ @data = data.end_with?("\n") ? data : data.concat("\n")
77
77
  @line_sizes = [0] + data.split("\n").map { it.length + 1 }
78
78
  begin
79
79
  @stream = Psych.parse_stream(data)
@@ -29,7 +29,7 @@ module PatchYAML
29
29
 
30
30
  [find_node(node, part), node]
31
31
  when NilClass
32
- return []
32
+ []
33
33
  else
34
34
  raise "patchyaml: Unexpected node #{from.first.class} in #find_node"
35
35
  end
@@ -104,6 +104,22 @@ module PatchYAML
104
104
  end
105
105
  end
106
106
 
107
+ def detect_stripped_whitespace(from)
108
+ stripped = []
109
+ loop do
110
+ case @data[from]
111
+ when " ", "\t"
112
+ stripped << @data[from]
113
+ from -= 1
114
+ when "\n"
115
+ stripped << "\n"
116
+ return stripped.reverse.join
117
+ else
118
+ return ""
119
+ end
120
+ end
121
+ end
122
+
107
123
  # TODO: update_* does not take into consideration adding a complex, multiline
108
124
  # value into an inline parent. This will certainly break things.
109
125
  def update_mapping(value, parent, new_value)
@@ -111,21 +127,34 @@ module PatchYAML
111
127
  key = parent.children[key_index]
112
128
  start_at = start_offset(value)
113
129
  end_at = end_offset(value)
114
- end_at -= 1 if @data[end_at - 1] == "\n"
115
130
  yaml_value = dump_yaml(new_value, indent: key.start_column + 2)
131
+ start_at -= 1 while @data[start_at - 1] == " "
132
+
133
+ # Psych assumes trailing whitespaces belong to the current value,
134
+ # which may cause newlines to be stripped when replacing a whole
135
+ # block. Here we detect how much was removed, so we can re-add it
136
+ # later when reassembling the file.
137
+ stripped = detect_stripped_whitespace(end_at - 1)
138
+
139
+ # rubocop:disable Lint/DuplicateBranch
116
140
  indent = case
117
141
  when new_value.is_a?(Hash)
118
- start_at -= 1 while @data[start_at - 1] == " "
119
142
  "\n#{" " * (key.start_column + 2)}"
120
- when (new_value.is_a?(Array) && parent.style != Psych::Nodes::Mapping::FLOW)
121
- start_at -= 1 while @data[start_at - 1] == " "
122
- "#{" " * (key.start_column + 2)}"
143
+ when new_value.is_a?(Array) && value.style == Psych::Nodes::Sequence::FLOW && value.children.empty?
144
+ "\n#{" " * (key.start_column + 2)}"
145
+ when new_value.is_a?(Array) && parent.style != Psych::Nodes::Mapping::FLOW
146
+ (" " * (key.start_column + 2)).to_s
147
+ when new_value.is_a?(String)
148
+ " "
123
149
  else
124
- ""
150
+ (" " * (key.start_column + 2)).to_s
125
151
  end
152
+ # rubocop:enable Lint/DuplicateBranch
153
+
126
154
  @data = @data[...start_at]
127
155
  .concat(indent)
128
156
  .concat(yaml_value)
157
+ .concat(stripped)
129
158
  .concat(@data[end_at...])
130
159
  end
131
160
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Patchyaml
4
- VERSION = "0.1.3"
5
- end
4
+ VERSION = "0.1.4"
5
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patchyaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vito Sartori
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-10-27 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: YAML Editor Utility Library
13
13
  email:
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubygems_version: 3.6.2
54
+ rubygems_version: 3.6.7
55
55
  specification_version: 4
56
56
  summary: YAML Editor Utility Library
57
57
  test_files: []