Class: RD::Tree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rd/tree.rb

Overview

document tree

Direct Known Subclasses

RDTree

Constant Summary collapse

SYSTEM_NAME =
"RDtool Framework -- Document Tree"
SYSTEM_VERSION =
"$Version: "+RD::VERSION+"$"
VERSION =
Version.new_from_version_string(SYSTEM_NAME, SYSTEM_VERSION)
TMP_DIR =
"/tmp"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document_struct, src_str = nil, include_paths = []) ⇒ Tree

Returns a new instance of Tree.



39
40
41
42
43
44
45
46
# File 'lib/rd/tree.rb', line 39

def initialize(document_struct, src_str = nil, include_paths = [])
  @src = src_str
  @document_struct = document_struct
  @include_paths = include_paths
  @filters = Hash.new()
  @tmp_dir = TMP_DIR
  @root = nil
end

Instance Attribute Details

#document_structObject (readonly)

Returns the value of attribute document_struct.



27
28
29
# File 'lib/rd/tree.rb', line 27

def document_struct
  @document_struct
end

#filtersObject (readonly) Also known as: filter

Returns the value of attribute filters.



31
32
33
# File 'lib/rd/tree.rb', line 31

def filters
  @filters
end

#include_pathsObject Also known as: include_path

Returns the value of attribute include_paths.



28
29
30
# File 'lib/rd/tree.rb', line 28

def include_paths
  @include_paths
end

#rootObject (readonly)

Returns the value of attribute root.



26
27
28
# File 'lib/rd/tree.rb', line 26

def root
  @root
end

#tmp_dirObject

Returns the value of attribute tmp_dir.



33
34
35
# File 'lib/rd/tree.rb', line 33

def tmp_dir
  @tmp_dir
end

Class Method Details

.new_from_rdo(*rdos) ⇒ Object

rdos: IOs



92
93
94
95
96
97
98
99
100
101
# File 'lib/rd/tree.rb', line 92

def Tree.new_from_rdo(*rdos) # rdos: IOs
  tree = Tree.new("", [], nil)
  tree_content = []
  rdos.each do |i|
	subtree = Marshal.load(i)
	tree_content.concat(subtree.root.blocks)
  end
  tree.root = DocumentElement.new(tree_content)
  tree
end

.new_with_document_struct(document_struct, include_paths = []) ⇒ Object



35
36
37
# File 'lib/rd/tree.rb', line 35

def Tree.new_with_document_struct(document_struct, include_paths = [])
  Tree.new(document_struct, nil, include_paths)
end

.tmp_dirObject



22
23
24
# File 'lib/rd/tree.rb', line 22

def Tree.tmp_dir
  TMP_DIR
end

.versionObject



16
17
18
# File 'lib/rd/tree.rb', line 16

def Tree.version
  VERSION
end

Instance Method Details

#accept(visitor) ⇒ Object



78
79
80
# File 'lib/rd/tree.rb', line 78

def accept(visitor)
  @root.accept(visitor)
end

#check_validObject



69
70
71
72
73
74
75
76
# File 'lib/rd/tree.rb', line 69

def check_valid
  each_element do |i|
	raise RuntimeError,
	  "mismatched document structure, #{i.parent} <-/- #{i}." unless
	  @document_struct.is_valid?(i.parent, i)
  end
  true
end

#each_element(&block) ⇒ Object Also known as: each



82
83
84
85
# File 'lib/rd/tree.rb', line 82

def each_element(&block)
  return nil unless @root
  @root.each(&block)
end

#make_root(&block) ⇒ Object



62
63
64
65
66
67
# File 'lib/rd/tree.rb', line 62

def make_root(&block)
  child = DocumentElement.new
  set_root(child)
  child.build(&block) if block_given?
  child
end

#parseObject



48
49
50
51
52
# File 'lib/rd/tree.rb', line 48

def parse
  parser = RDParser.new
  src = @src.respond_to?(:to_a) ? @src.to_a : @src.split(/^/)
  set_root(parser.parse(src, self))
end

#set_root(element) ⇒ Object Also known as: root=

Raises:

  • (ArgumentError)


54
55
56
57
58
59
# File 'lib/rd/tree.rb', line 54

def set_root(element)
  raise ArgumentError, "#{element.class} can't be root." unless
	@document_struct.is_valid?(self, element)
  @root = element
  element.parent = self
end

#treeObject



88
89
90
# File 'lib/rd/tree.rb', line 88

def tree
  self
end