Created
November 4, 2019 19:29
-
-
Save we4tech/438bf2f64e9c37cac023ede14bc2d31c to your computer and use it in GitHub Desktop.
How to use double quoted a string node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class YAMLPreservedStringQuote | |
def initialize(*doublequoted_nodes) | |
@doublequoted_nodes = doublequoted_nodes | |
end | |
def load(str) | |
ast = YAML.parse_stream(StringIO.new(str)) | |
ast.grep(Psych::Nodes::Mapping).each do |node| | |
node.children.each_slice(2) do |key, value| | |
next unless @doublequoted_nodes.include?(key.value) | |
if value.is_a?(Psych::Nodes::Sequence) | |
value.children.each(&method(:double_quote!)) | |
else | |
double_quote!(value) | |
end | |
end | |
end | |
ast | |
end | |
private | |
def double_quote!(node) | |
node.plain = false | |
node.quoted = true | |
node.style = Psych::Nodes::Scalar::DOUBLE_QUOTED | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment