Class: TreeDiff::Node

Inherits:
Struct
  • Object
show all
Defined in:
lib/tree_diff/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children

Returns:

  • (Object)

    the current value of children



2
3
4
# File 'lib/tree_diff/node.rb', line 2

def children
  @children
end

#edgeObject

Returns the value of attribute edge

Returns:

  • (Object)

    the current value of edge



2
3
4
# File 'lib/tree_diff/node.rb', line 2

def edge
  @edge
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/tree_diff/node.rb', line 2

def name
  @name
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



2
3
4
# File 'lib/tree_diff/node.rb', line 2

def source
  @source
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



3
4
5
# File 'lib/tree_diff/node.rb', line 3

def == other
  name == other.name && edge == other.edge
end

#accept(visitor) ⇒ Object



25
26
27
# File 'lib/tree_diff/node.rb', line 25

def accept visitor
  visitor.visit(self)
end

#all_equal?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tree_diff/node.rb', line 35

def all_equal?
  eql = Class.new {
    attr_accessor :equal

    def initialize root
      @root   = root
      @equal  = true
    end

    def accept target
      target.accept(self)
    end

    def visit node
      return unless @equal
      @equal = node.source == @root.source
      node.children.each { |c| c.accept(self) }
    end
  }.new(self)
  eql.accept(self)
  eql.equal
end

#hashObject



8
9
10
# File 'lib/tree_diff/node.rb', line 8

def hash
  "#{name}#{edge}".hash
end

#merge(other) ⇒ Object



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

def merge other
  raise unless other.name == name
  diff   = (children - other.children) + (other.children - children)
  
  left_same   = (children & other.children)
  
  merged = left_same.map do |node|
    node.merge(other.children.find { |n| n == node })
  end
  
  Node.new(name, edge, diff + merged, source + other.source)
end

#to_dotObject



29
30
31
32
33
# File 'lib/tree_diff/node.rb', line 29

def to_dot
  dv = DotVisitor.new(self)
  dv.accept(self)
  dv
end