Class: Sus::Tree
- Inherits:
-
Object
- Object
- Sus::Tree
- Defined in:
- lib/sus/tree.rb
Overview
Represents a tree structure of test contexts.
Instance Method Summary collapse
-
#initialize(context) ⇒ Tree
constructor
Initialize a new Tree.
-
#to_json(options = nil) ⇒ Object
Convert the tree to JSON.
-
#traverse(current = @context, &block) ⇒ Object
Traverse the tree, yielding each context.
Constructor Details
#initialize(context) ⇒ Tree
Initialize a new Tree.
11 12 13 |
# File 'lib/sus/tree.rb', line 11 def initialize(context) @context = context end |
Instance Method Details
#to_json(options = nil) ⇒ Object
Convert the tree to JSON.
36 37 38 39 40 |
# File 'lib/sus/tree.rb', line 36 def to_json( = nil) traverse do |context| [context.identity.to_s, context.description.to_s, context.leaf?] end.to_json() end |
#traverse(current = @context, &block) ⇒ Object
Traverse the tree, yielding each context.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sus/tree.rb', line 19 def traverse(current = @context, &block) node = {} node[:self] = yield(current) if children = current.children # and children.any? node[:children] = children.values.map do |context| self.traverse(context, &block) end end return node end |