Module: HasShortestPath::ClassMethods
- Defined in:
- lib/has_shortest_path.rb
Instance Method Summary collapse
-
#has_shortest_path(options = {}) ⇒ Object
has_shortest_pathdefines a class as an edge in a graph.
Instance Method Details
#has_shortest_path(options = {}) ⇒ Object
has_shortest_path defines a class as an edge in a graph
-
via: the class’s accessor for edges that it is connected to -
weighted_with: the class’s accessor for the weight variable -
through: the intermediate model that contains the weight (this is the vertex model)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/has_shortest_path.rb', line 18 def has_shortest_path = {} cattr_accessor :shortest_path_opts cattr_accessor :weighted_array self.shortest_path_opts = raise "has_shortest_path requires the arguments :via, :weighted_with, and :through." if shortest_path_opts[:via].nil? || shortest_path_opts[:through].nil? || shortest_path_opts[:weighted_with].nil? # Weight of the vertex origination.destinations[1] => origination.connections[1].cost # Set up a method like 'weighted_destinations' that will return an array of all of the edges we have a vertex to, # with each edge having the weight from this edge to that edge. attr_accessor shortest_path_opts[:weighted_with] self.weighted_array = "weighted_#{self.shortest_path_opts[:via].to_s}".to_sym define_method(weighted_array) do self.send(shortest_path_opts[:via]).enum_for(:each_with_index).map { |dest, i| dest.cost = self.send(shortest_path_opts[:through])[i].cost; dest } end end |