Class: Sus::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/tree.rb

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Tree

Returns a new instance of Tree.



8
9
10
# File 'lib/sus/tree.rb', line 8

def initialize(context)
	@context = context
end

Instance Method Details

#to_json(options = nil) ⇒ Object



26
27
28
29
30
# File 'lib/sus/tree.rb', line 26

def to_json(options = nil)
	traverse do |context|
		[context.identity.to_s, context.description.to_s, context.leaf?]
	end.to_json(options)
end

#traverse(current = @context, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sus/tree.rb', line 12

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