Class: Mochigome::ModelGraph
- Inherits:
-
Object
- Object
- Mochigome::ModelGraph
- Defined in:
- lib/model_graph.rb
Instance Method Summary collapse
- #edge_condition(u, v) ⇒ Object
-
#expr_models(e) ⇒ Object
Take an expression and return a Set of all models it references.
-
#initialize ⇒ ModelGraph
constructor
A new instance of ModelGraph.
- #path_thru(models) ⇒ Object
- #relation_init(model) ⇒ Object
Constructor Details
#initialize ⇒ ModelGraph
Returns a new instance of ModelGraph.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/model_graph.rb', line 10 def initialize @graphed_models = Set.new @table_to_model = {} @assoc_graph = RGL::DirectedAdjacencyGraph.new(OrderedSet) # TODO Also maybe need to do this with hashes used internally in traversal @assoc_graph.instance_variable_set( # Make path choice more predictable :@vertice_dict, ActiveSupport::OrderedHash.new ) @edge_conditions = {} @shortest_paths = {} end |
Instance Method Details
#edge_condition(u, v) ⇒ Object
61 62 63 |
# File 'lib/model_graph.rb', line 61 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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/model_graph.rb', line 24 def expr_models(e) r = Set.new if e.is_a?(Array) e.each{|sub_e| r += expr_models(sub_e)} return r end [:expr, :left, :right, :expressions].each do |m| r += expr_models(e.send(m)) if e.respond_to?(m) end if e.respond_to?(:relation) model = nil begin model = @table_to_model[e.relation.name] or raise ModelSetupError.new("Table lookup error: #{e.relation.name}") rescue ModelSetupError Dir.glob(RAILS_ROOT + '/app/models/*.rb').each do |path| clsname = File.basename(path).sub(/\.rb$/, "").classify require File.(path) unless Object.const_defined?(clsname) end Object.subclasses_of(ActiveRecord::Base).each do |m| if m.table_name == e.relation.name model = m break end end raise unless model end r.add model end r end |
#path_thru(models) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/model_graph.rb', line 65 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
56 57 58 59 |
# File 'lib/model_graph.rb', line 56 def relation_init(model) update_assoc_graph([model]) model.arel_table.project # Project to convert Arel::Table to Arel::Rel end |