Module: MiniGauge::ClassMethods

Defined in:
lib/mini_gauge.rb

Instance Method Summary collapse

Instance Method Details

#add_to_graph(graph) ⇒ Object



275
276
277
278
# File 'lib/mini_gauge.rb', line 275

def add_to_graph(graph)
  graph.nodes << {:name => self.dot_node_name, :attributes => self.dot_node_attributes}
  self.fill_with_relations(graph)
end

#dot_node_attributesObject

Returns the attributes of this node for dot graphs



221
222
223
224
225
226
227
228
229
230
231
# File 'lib/mini_gauge.rb', line 221

def dot_node_attributes
  if self.table_exists?
    hidden_fields = MiniGauge::HIDDEN_FIELDS << "#{self.table_name}_count"

    return self.content_columns.reject{|x| hidden_fields.include?(x.name)}.collect{ |col|
      "#{col.name} :#{col.type.to_s}"
    }
  else
    return ["Table doesn't exist"]
  end
end

#dot_node_nameObject

Returns the name of this for graph nodes



216
217
218
# File 'lib/mini_gauge.rb', line 216

def dot_node_name
  self.name
end

#fill_with_relations(dot_graph) ⇒ Object

Accepts a Graph object and fills it with the relations for this object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/mini_gauge.rb', line 234

def fill_with_relations(dot_graph)
  self.reflect_on_all_associations.each do |assoc|
    
    if assoc.class_name == assoc.name.to_s.singularize.camelize
      assoc_name = ''
    else
      assoc_name = assoc.name.to_s
    end
    
    if assoc.macro.to_s == 'has_one' || assoc.macro.to_s == 'belongs_to'
      assoc_type = 'one-one'
    elsif assoc.macro.to_s == 'has_many' && (! assoc.options[:through])
      assoc_type = 'one-many'
    else # habtm or has_many, :through
      assoc_type = 'many-many'
    end 
    
    if assoc.options[:polymorphic]
      dot_graph.nodes << {:name => assoc.class_name, :attributes => ["polymorphic record"], :options => {:color => "gray61"}}
      dot_graph.edges << {:empty_rec => true, :source => self.dot_node_name, :destination => assoc.class_name, :type => assoc_type, :label => assoc_name }
    else
      dot_graph.nodes << {:name => assoc.klass.dot_node_name, :attributes => assoc.klass.dot_node_attributes}
      dot_graph.edges << {:source => self.dot_node_name, :destination => assoc.klass.dot_node_name, :type => assoc_type, :label => assoc_name }
    end
    
  end
end

#to_dot_notation(opts = {}) {|@graph| ... } ⇒ Object

Creats a dot node graph and fills it with the relations for this object. Returns a string representing the graph. Will accept a block which passes the Graph object in case there are other relations to add.

Yields:

  • (@graph)


264
265
266
267
268
269
270
271
272
273
# File 'lib/mini_gauge.rb', line 264

def to_dot_notation(opts = {})
  @graph = MiniGauge::Graph.new(opts)
  
  self.add_to_graph(@graph)
  
  yield(@graph) if block_given?
  
  return @graph.to_dot_notation

end