Module: Dagnabit::Vertex::Bonding
- Defined in:
- lib/dagnabit/vertex/bonding.rb
Overview
Contains methods for bonding a vertex to a graph.
Instance Method Summary collapse
-
#bond_for(graph) ⇒ Array<edge_model>
Calculates a bond for this vertex (the method receiver) to all source vertices of ‘graph`.
Instance Method Details
#bond_for(graph) ⇒ Array<edge_model>
Calculates a bond for this vertex (the method receiver) to all source vertices of ‘graph`.
This method expects the receiver to have been persisted, and will raise ‘RuntimeError` if that is not the case.
Persisted edges that already connect the receiver vertex to source vertices in ‘graph` will not be duplicated. Unpersisted edges connecting the receiver to a source in `graph` will be duplicated, so it is advised that you ensure that all vertices and edges in `graph` have been persisted before using #bond_for.
This method requires the existence of an edge model; see Settings#edge_model and Settings#edge_model_name=. If an edge model has not been set, this method raises ‘RuntimeError`.
Edges instantiated by ‘bond_for` are not automatically saved.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dagnabit/vertex/bonding.rb', line 30 def bond_for(graph) edge = self.class.edge_model raise 'edge_model must be set' unless edge raise "#{self} must be persisted before invoking bond_for" if new_record? sources = graph.sources existing = edge.all(:conditions => { :parent_id => id, :child_id => sources.map(&:id) }).map { |e| [e.parent_id, e.child_id] } new = sources.inject([]) { |es, s| es << [id, s.id] } (new - existing).map { |p, c| edge.new(:parent_id => p, :child_id => c) } end |