patchyaml 0.1.2 → 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: '09030d2cd8460fc135a5b74cc74277933efb7b9ba90c2cfa59fe14ea5de7496e'
4
- data.tar.gz: 2ba8dddffcd1aa85adcaf10ae2aa92583a7ab10b61eb6c09be6771b9947adb33
3
+ metadata.gz: 27d24b69a097c75a45afb302b0b4199b9bc2ed45bd34d8cf141717def23e6f03
4
+ data.tar.gz: ec42ec73c55a184ee211cc1b9fbc3f907380d85fa2b5fc951bdaeb2abde66e69
5
5
  SHA512:
6
- metadata.gz: 7e4d2de2e5ea6b51a40788003ca8284a5c73a025ddcc12c5f4cecb59359809ed055423e119fa2f7e16651ecdd93936feeca3cc1552b1db9a800f9965d7e31508
7
- data.tar.gz: 0603fc03c1bbc826ddd80ec3f04d9cbf1d9a75ab065ce62e0485ad307add6146cf8ac9ef82430933aad3f02d02516343f6fb1cba33884fced9e447f98bf99b6a
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)
@@ -112,15 +128,33 @@ module PatchYAML
112
128
  start_at = start_offset(value)
113
129
  end_at = end_offset(value)
114
130
  yaml_value = dump_yaml(new_value, indent: key.start_column + 2)
115
- indent = if new_value.is_a?(Hash) || (new_value.is_a?(Array) && parent.style != Psych::Nodes::Mapping::FLOW)
116
- start_at -= 1 while @data[start_at - 1] == " "
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
140
+ indent = case
141
+ when new_value.is_a?(Hash)
117
142
  "\n#{" " * (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
+ " "
118
149
  else
119
- ""
150
+ (" " * (key.start_column + 2)).to_s
120
151
  end
152
+ # rubocop:enable Lint/DuplicateBranch
153
+
121
154
  @data = @data[...start_at]
122
155
  .concat(indent)
123
156
  .concat(yaml_value)
157
+ .concat(stripped)
124
158
  .concat(@data[end_at...])
125
159
  end
126
160
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Patchyaml
4
- VERSION = "0.1.2"
5
- end
4
+ VERSION = "0.1.4"
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patchyaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vito Sartori