Module: Dag::Edges

Defined in:
lib/dag/edges.rb

Defined Under Namespace

Modules: EdgeInstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/dag/edges.rb', line 4

def self.included(base)
  base.send :include, EdgeInstanceMethods
end

Instance Method Details

#build_edge(ancestor, descendant) ⇒ Object

Class methods that extend the link model for both polymorphic and non-polymorphic graphs Returns a new edge between two points



10
11
12
13
14
15
16
17
# File 'lib/dag/edges.rb', line 10

def build_edge(ancestor, descendant)
  source = self::EndPoint.from(ancestor)
  sink = self::EndPoint.from(descendant)
  conditions = self.conditions_for(source, sink)
  path = self.new(conditions)
  path.make_direct
  path
end

#create_edge(ancestor, descendant) ⇒ Object

Creates an edge between two points using save



41
42
43
44
45
46
47
48
49
50
# File 'lib/dag/edges.rb', line 41

def create_edge(ancestor, descendant)
  link = self.find_link(ancestor, descendant)
  if link.nil?
    edge = self.build_edge(ancestor, descendant)
    return edge.save
  else
    link.make_direct
    return link.save
  end
end

#create_edge!(ancestor, descendant) ⇒ Object

Creates an edge between two points using save! Returns created edge



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dag/edges.rb', line 53

def create_edge!(ancestor, descendant)
  link = self.find_link(ancestor, descendant)
  if link.nil?
    edge = self.build_edge(ancestor, descendant)
    edge.save!
    edge
  else
    link.make_direct
    link.save!
    link
  end
end

#direct?(ancestor, descendant) ⇒ Boolean

Alias for edge

Returns:

  • (Boolean)


116
117
118
# File 'lib/dag/edges.rb', line 116

def direct?(ancestor, descendant)
  self.edge?(ancestor, descendant)
end

#edge?(ancestor, descendant) ⇒ Boolean

Determines if an edge exists between two points

Returns:

  • (Boolean)


111
112
113
# File 'lib/dag/edges.rb', line 111

def edge?(ancestor, descendant)
  !self.find_edge(ancestor, descendant).nil?
end

#find_edge(ancestor, descendant) ⇒ Object

Finds an edge between two points, Must be direct



20
21
22
23
24
# File 'lib/dag/edges.rb', line 20

def find_edge(ancestor, descendant)
  source = self::EndPoint.from(ancestor)
  sink = self::EndPoint.from(descendant)
  self.first :conditions => self.conditions_for(source, sink).merge!({direct_column_name => true})
end

Finds a link between two points



27
28
29
30
31
# File 'lib/dag/edges.rb', line 27

def find_link(ancestor, descendant)
  source = self::EndPoint.from(ancestor)
  sink = self::EndPoint.from(descendant)
  self.first :conditions => self.conditions_for(source, sink)
end

#find_or_build_edge(ancestor, descendant) ⇒ Object

Finds or builds an edge between two points



34
35
36
37
38
# File 'lib/dag/edges.rb', line 34

def find_or_build_edge(ancestor, descendant)
  edge = self.find_edge(ancestor, descendant)
  return edge unless edge.nil?
  return build_edge(ancestor, descendant)
end

#longest_path_between(ancestor, descendant, path = []) ⇒ Object

Finds the longest path between ancestor and descendant returning as an array



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dag/edges.rb', line 67

def longest_path_between(ancestor, descendant, path=[])
  longest = []
  ancestor.children.each do |child|
    if child == descendant
      temp = path.clone
      temp << child
      if temp.length > longest.length
        longest = temp
      end
    elsif self.find_link(child, descendant)
      temp = path.clone
      temp << child
      temp = self.longest_path_between(child, descendant, temp)
      if temp.length > longest.length
        longest = temp
      end
    end
  end
  longest
end

#shortest_path_between(ancestor, descendant, path = []) ⇒ Object

Finds the shortest path between ancestor and descendant returning as an array



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dag/edges.rb', line 89

def shortest_path_between(ancestor, descendant, path=[])
  shortest = []
  ancestor.children.each do |child|
    if child == descendant
      temp = path.clone
      temp << child
      if shortest.blank? || temp.length < shortest.length
        shortest = temp
      end
    elsif self.find_link(child, descendant)
      temp = path.clone
      temp << child
      temp = self.shortest_path_between(child, descendant, temp)
      if shortest.blank? || temp.length < shortest.length
        shortest = temp
      end
    end
  end
  return shortest
end