Class: GraphBuilder::Base

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

Overview

implements an AND/OR tree An Branch represents a branch An Chain represents a chain A Leaf contains a thing

Direct Known Subclasses

Leaf, Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, opts = {}) ⇒ Base

Returns a new instance of Base.



13
14
15
# File 'lib/graph_builder.rb', line 13

def initialize parent, opts = {}
  @parent, @opts, @children = parent, opts, []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



11
12
13
# File 'lib/graph_builder.rb', line 11

def children
  @children
end

#optsObject (readonly)

Returns the value of attribute opts.



11
12
13
# File 'lib/graph_builder.rb', line 11

def opts
  @opts
end

#parentObject (readonly)

Returns the value of attribute parent.



11
12
13
# File 'lib/graph_builder.rb', line 11

def parent
  @parent
end

Instance Method Details

#ancestorsObject



33
# File 'lib/graph_builder.rb', line 33

def ancestors; parent ? parent.ancestors << parent : [];  end

#debug(&proc) ⇒ Object



25
26
27
28
29
# File 'lib/graph_builder.rb', line 25

def debug &proc 
  DEBUG or return false
  puts indent + proc.call
  true
end

#depthObject



32
# File 'lib/graph_builder.rb', line 32

def depth;     ancestors.count; end

#dump(recursive = true) ⇒ Object



20
21
22
23
# File 'lib/graph_builder.rb', line 20

def dump recursive = true
  debug{self.to_s} or return false
  children.each{|c|c.dump(recursive)} if recursive
end

#empty?Boolean

Returns:

  • (Boolean)


18
# File 'lib/graph_builder.rb', line 18

def empty?;  children.all? &:empty?;  end

#indentObject



31
# File 'lib/graph_builder.rb', line 31

def indent;    "   " * depth;   end

#processObject



17
# File 'lib/graph_builder.rb', line 17

def process; children.each &:process; end