Skip to content

Commit f4ab24f

Browse files
committed
add the ability to update leading and trailling comments (wantedly#6)
1 parent c913094 commit f4ab24f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/psych/comments/node_ext.rb

+8
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ def leading_comments
88
def trailing_comments
99
@trailing_comments ||= []
1010
end
11+
12+
def leading_comments=(comments)
13+
@leading_comments = Array(comments)
14+
end
15+
16+
def trailing_comments=(comments)
17+
@trailing_comments = Array(comments)
18+
end
1119
end

spec/node_ext_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,36 @@
88
node = Psych::Nodes::Scalar.new("foo")
99
expect(node.leading_comments).to eq([])
1010
end
11+
12+
it "can be set with an array of comments" do
13+
node = Psych::Nodes::Scalar.new("foo")
14+
node.leading_comments = ["# Comment 1", "# Comment 2"]
15+
expect(node.leading_comments).to eq(["# Comment 1", "# Comment 2"])
16+
end
17+
18+
it "wraps non-array values in an array" do
19+
node = Psych::Nodes::Scalar.new("foo")
20+
node.leading_comments = "# Single comment"
21+
expect(node.leading_comments).to eq(["# Single comment"])
22+
end
1123
end
1224

1325
describe "#trailing_comments" do
1426
it "has an array" do
1527
node = Psych::Nodes::Scalar.new("foo")
1628
expect(node.trailing_comments).to eq([])
1729
end
30+
31+
it "can be set with an array of comments" do
32+
node = Psych::Nodes::Scalar.new("foo")
33+
node.trailing_comments = ["# Comment 1", "# Comment 2"]
34+
expect(node.trailing_comments).to eq(["# Comment 1", "# Comment 2"])
35+
end
36+
37+
it "wraps non-array values in an array" do
38+
node = Psych::Nodes::Scalar.new("foo")
39+
node.trailing_comments = "# Single comment"
40+
expect(node.trailing_comments).to eq(["# Single comment"])
41+
end
1842
end
1943
end

0 commit comments

Comments
 (0)