diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2017-12-20 09:22:15 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2017-12-20 09:22:15 -0800 |
commit | 3628243bc0af1eb19f1844c7e22da6aa6d46e074 (patch) | |
tree | 5777c44e8b32e93c0c97875c9b2d5933a3d9106e | |
parent | 39b1e826d2e505fb35022fd9bf4f3a6dc0ee5702 (diff) | |
download | psych-3628243bc0af1eb19f1844c7e22da6aa6d46e074.tar.gz |
Add a predicate method to each node
This allows the AST to be searched via a predicate method rather than
hardcoding the class name and doing is_a? checks. For example, rather
than:
```
ast.grep(Psych::Nodes::Scalar).each do |node|
# .. do something
end
```
Now you can do:
```
ast.find_all(&:scalar?).each do |node|
# .. do something
end
```
Your code no longer needs to know the exact class used in the AST.
-rw-r--r-- | lib/psych/nodes/alias.rb | 2 | ||||
-rw-r--r-- | lib/psych/nodes/document.rb | 2 | ||||
-rw-r--r-- | lib/psych/nodes/mapping.rb | 2 | ||||
-rw-r--r-- | lib/psych/nodes/node.rb | 7 | ||||
-rw-r--r-- | lib/psych/nodes/scalar.rb | 2 | ||||
-rw-r--r-- | lib/psych/nodes/sequence.rb | 2 | ||||
-rw-r--r-- | lib/psych/nodes/stream.rb | 2 | ||||
-rw-r--r-- | test/psych/test_stream.rb | 16 |
8 files changed, 35 insertions, 0 deletions
diff --git a/lib/psych/nodes/alias.rb b/lib/psych/nodes/alias.rb index 8131a4b..6da655f 100644 --- a/lib/psych/nodes/alias.rb +++ b/lib/psych/nodes/alias.rb @@ -14,6 +14,8 @@ module Psych def initialize anchor @anchor = anchor end + + def alias?; true; end end end end diff --git a/lib/psych/nodes/document.rb b/lib/psych/nodes/document.rb index 3cd418e..f57410d 100644 --- a/lib/psych/nodes/document.rb +++ b/lib/psych/nodes/document.rb @@ -56,6 +56,8 @@ module Psych def root children.first end + + def document?; true; end end end end diff --git a/lib/psych/nodes/mapping.rb b/lib/psych/nodes/mapping.rb index b921ddc..d49678c 100644 --- a/lib/psych/nodes/mapping.rb +++ b/lib/psych/nodes/mapping.rb @@ -52,6 +52,8 @@ module Psych @implicit = implicit @style = style end + + def mapping?; true; end end end end diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb index 6d86669..f59fb89 100644 --- a/lib/psych/nodes/node.rb +++ b/lib/psych/nodes/node.rb @@ -63,6 +63,13 @@ module Psych io end alias :to_yaml :yaml + + def alias?; false; end + def document?; false; end + def mapping?; false; end + def scalar?; false; end + def sequence?; false; end + def stream?; false; end end end end diff --git a/lib/psych/nodes/scalar.rb b/lib/psych/nodes/scalar.rb index b448858..e2616b6 100644 --- a/lib/psych/nodes/scalar.rb +++ b/lib/psych/nodes/scalar.rb @@ -63,6 +63,8 @@ module Psych @quoted = quoted @style = style end + + def scalar?; true; end end end end diff --git a/lib/psych/nodes/sequence.rb b/lib/psych/nodes/sequence.rb index 77c2c60..740f193 100644 --- a/lib/psych/nodes/sequence.rb +++ b/lib/psych/nodes/sequence.rb @@ -77,6 +77,8 @@ module Psych @implicit = implicit @style = style end + + def sequence?; true; end end end end diff --git a/lib/psych/nodes/stream.rb b/lib/psych/nodes/stream.rb index 2474fe6..b525217 100644 --- a/lib/psych/nodes/stream.rb +++ b/lib/psych/nodes/stream.rb @@ -33,6 +33,8 @@ module Psych super() @encoding = encoding end + + def stream?; true; end end end end diff --git a/test/psych/test_stream.rb b/test/psych/test_stream.rb index 3bd557c..9b71c6d 100644 --- a/test/psych/test_stream.rb +++ b/test/psych/test_stream.rb @@ -3,6 +3,22 @@ require_relative 'helper' module Psych class TestStream < TestCase + [ + [Psych::Nodes::Alias, :alias?], + [Psych::Nodes::Document, :document?], + [Psych::Nodes::Mapping, :mapping?], + [Psych::Nodes::Scalar, :scalar?], + [Psych::Nodes::Sequence, :sequence?], + [Psych::Nodes::Stream, :stream?], + ].each do |klass, block| + define_method :"test_predicate_#{block}" do + rb = Psych.parse_stream("---\n- foo: bar\n- &a !!str Anchored\n- *a") + nodes = rb.grep(klass) + assert_operator nodes.length, :>, 0 + assert_equal nodes, rb.find_all(&block) + end + end + def test_parse_partial rb = Psych.parse("--- foo\n...\n--- `").to_ruby assert_equal 'foo', rb |