Module: MiniGauge::InstanceMethods

Defined in:
lib/mini_gauge.rb

Instance Method Summary collapse

Instance Method Details

#dot_node_attributesObject

Returns a string used as the node description in the graph by inspecting the instanece attributes



290
291
292
293
294
295
296
297
298
# File 'lib/mini_gauge.rb', line 290

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

  return self.attributes.reject{|k,v| hidden_fields.include?(k) || v.nil? }.collect{ |k,v|
    "#{k}: #{v.to_s.gsub(/['"]/,'')}"
  }
  
end

#dot_node_definitionObject

Returns a node definition for the current object. For example:



301
302
303
# File 'lib/mini_gauge.rb', line 301

def dot_node_definition
  {:name => self.dot_node_name, :label => self.dot_node_name, :attributes => self.dot_node_attributes }
end

#dot_node_nameObject

Returns a node name for the current object, used when creating relations and in definitions



285
286
287
# File 'lib/mini_gauge.rb', line 285

def dot_node_name
  "#{self.class.name.to_s}_#{self.id || self.object_id}"
end

#fill_dot_graph(dot_graph, opts = {}) ⇒ Object

Takes a graph and fills in the graph given the options by inspecting the relations



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/mini_gauge.rb', line 325

def fill_dot_graph(dot_graph, opts={})
  
  dot_graph.nodes << self.dot_node_definition
  
  # Set up to allow something like this:
  #     :include => [:product, {:invoice_item => :invoice}]
  # ie, about the same as ar associations
  opts[:include] = Array(opts[:include]) unless opts[:include].respond_to?(:each)
  opts[:include].each do |relation, relation_opts|
    if relation.is_a?(Hash) || relation.is_a?(Array)
      relation.each do |name, sub_opts|
        # We do the funny bits with data here because calling Array() on it makes AR complain
        data = self.send(name.to_sym) 
        data = [data] unless data.respond_to?(:each)
        data = [nil] if data.empty?  #make sure we have something to show for this leg of the graph
        data.each do |obj|
          if obj.nil? # It was in the options, but there is nothing found for it, eg self.product returns nil
            dot_graph.add(:destination => obj, :source => self, :label => name.to_s)
          else
            # Here is where we recurse, rolling everything into our list of nodes and edges.
            obj.fill_dot_graph(dot_graph, :include => sub_opts) 
            dot_graph.edges << {:destination => obj.dot_node_name, :source => dot_node_name, :label => name.to_s.humanize}
          end
        end
      end
    else
      data = self.send(relation.to_sym)
      data = [data] unless data.respond_to?(:each)
      data = [nil] if data.empty? #make sure we have something to show for this leg of the graph
      data.each do |obj|
        if relation_opts #more recursion here
          obj.fill_dot_graph(dot_graph, :include => relation_opts)
          dot_graph.edges << {:destination => obj.dot_node_name, :source => dot_node_name, :label => relation.to_s.humanize}
        else
          dot_graph.add(:destination => obj, :source => self, :label => relation.to_s.humanize)
        end
      end
    end
  end
  
end

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

Optionally pass a block to manually fill in details.

@org_membership.to_dot_notation(:include => => :people, :title => “An organization with a transferred Contact Representative”, :description => “Organizations can transfer Contact Representative memberships to other people. This graph shows what the data looks like after a transfer” ) do |graph|

@org_membership.member_products.contact_reps.each do |cr_mpr|
  graph.add(:source => @org_membership, :destination => cr_mpr, :label => "member_products.contact_reps")
  cr_mpr.fill_dot_graph(graph, :include => [{:product => :member}, {:invoice_item => :invoice}])
end

end

Yields:

  • (@graph)


313
314
315
316
317
318
319
320
321
322
# File 'lib/mini_gauge.rb', line 313

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

end