File: document-struct.rb

package info (click to toggle)
rdtool 0.6.38-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 940 kB
  • sloc: ruby: 8,213; lisp: 387; sh: 33; makefile: 16
file content (46 lines) | stat: -rw-r--r-- 973 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

module RD
  # DocumentStructure defines and restricts structure of document tree.
  # it consists of ElementRelationship
  class DocumentStructure
    def initialize
      @relationships = []
    end
    
    def add_relationships(*relations)
      @relationships += relations
    end

    def define_relationship(parent, child)
      add_relationships(ElementRelationship.new(parent, child))
    end

    def each_relationship
      @relationships.each do |i|
	yield(i)
      end
    end

    def is_valid?(parent, child)
      each_relationship do |i|
	return true if i.match?(parent, child)
      end
      false
    end
  end

  # ElementRelationship is knowledge about parent-children relationship
  # between Elements.
  class ElementRelationship
    attr_reader(:parent, :child)

    def initialize(parent, child)
      @parent = parent
      @child = child
    end

    def match?(parent, child)
      parent.is_a? @parent and child.is_a? @child
    end
  end
end