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 +4 -4
- data/lib/patchyaml/editor.rb +2 -2
- data/lib/patchyaml/find.rb +1 -1
- data/lib/patchyaml/pipeline.rb +37 -3
- data/lib/patchyaml/version.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 27d24b69a097c75a45afb302b0b4199b9bc2ed45bd34d8cf141717def23e6f03
|
|
4
|
+
data.tar.gz: ec42ec73c55a184ee211cc1b9fbc3f907380d85fa2b5fc951bdaeb2abde66e69
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 579bec7e6859c1befd69cebcbf7e67d1ab7c4352bfd377160838fbd89d22d98602d251e64772d26d1428fa9e801202cd881ed7e2d35625b5e499a7498328c3f2
|
|
7
|
+
data.tar.gz: c0e4cdc1e179f6832663a102585e838a79f7083f8c7f1abad058685214ef5af8c2b3f1531acaf40543aeb8cdf6fe3fa77b856e0b1a957f087aafdc3ad44ea700
|
data/lib/patchyaml/editor.rb
CHANGED
|
@@ -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,
|
|
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)
|
data/lib/patchyaml/find.rb
CHANGED
data/lib/patchyaml/pipeline.rb
CHANGED
|
@@ -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
|
-
|
|
116
|
-
|
|
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
|
|
data/lib/patchyaml/version.rb
CHANGED