Class: Mochigome::ModelGraph

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

Instance Method Summary collapse

Constructor Details

#initializeModelGraph

Returns a new instance of ModelGraph.



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

def initialize
  @graphed_models = Set.new
  @table_to_model = {}
  @assoc_graph = RGL::DirectedAdjacencyGraph.new
  @edge_conditions = {}
  @shortest_paths = {}
end

Instance Method Details

#edge_condition(u, v) ⇒ Object



37
38
39
# File 'lib/model_graph.rb', line 37

def edge_condition(u, v)
  @edge_conditions[[u,v]]
end

#expr_models(e) ⇒ Object

Take an expression and return a Set of all models it references



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/model_graph.rb', line 19

def expr_models(e)
  r = Set.new
  [:expr, :left, :right].each do |m|
    r += expr_models(e.send(m)) if e.respond_to?(m)
  end
  if e.respond_to?(:relation)
    model = @table_to_model[e.relation.name] or
      raise ModelSetupError.new("Table lookup error: #{e.relation.name}")
    r.add model
  end
  r
end

#path_thru(models) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/model_graph.rb', line 41

def path_thru(models)
  update_assoc_graph(models)
  model_queue = models.dup
  path = [model_queue.shift]
  until model_queue.empty?
    src = path.last
    tgt = model_queue.shift
    next if src == tgt
    real_src = src.to_real_model
    real_tgt = tgt.to_real_model
    unless real_src == real_tgt
      seg = @shortest_paths[[real_src,real_tgt]]
      return nil unless seg
      path.concat seg.take(seg.size-1).drop(1)
    end
    path << tgt
  end

  # Don't return any paths that double back
  return nil unless path.uniq.size == path.size
  path
end

#relation_init(model) ⇒ Object



32
33
34
35
# File 'lib/model_graph.rb', line 32

def relation_init(model)
  update_assoc_graph([model])
  model.arel_table.project # Project to convert Arel::Table to Arel::Rel
end