Module: Dagnabit::Link::ClassMethods

Defined in:
lib/dagnabit/link/class_methods.rb

Overview

Handy class methods for creating and querying paths.

Instance Method Summary collapse

Instance Method Details

#build_edge(from, to, attributes = {}) ⇒ Object

Constructs a new edge. The direction of the edge runs from from to to.



10
11
12
# File 'lib/dagnabit/link/class_methods.rb', line 10

def build_edge(from, to, attributes = {})
  new(attributes.merge(:ancestor => from, :descendant => to))
end

#connect(from, to, attributes = {}) ⇒ Object

Like build_edge, but saves the edge after it is instantiated. Returns true if the endpoints could be connected, false otherwise.

See Dagnabit::Link::Validations for more information on built-in link validations.



21
22
23
# File 'lib/dagnabit/link/class_methods.rb', line 21

def connect(from, to, attributes = {})
  build_edge(from, to, attributes).save
end

#path?(a, b) ⇒ Boolean

Returns true if there is a path from a to b, false otherwise.

Returns:

  • (Boolean)


28
29
30
# File 'lib/dagnabit/link/class_methods.rb', line 28

def path?(a, b)
  paths(a, b).count > 0
end

#paths(a, b) ⇒ Object

Returns all paths from a to b.

These paths are returned as transitive closure links, which aren’t guaranteed to have the same methods as your link class.



38
39
40
# File 'lib/dagnabit/link/class_methods.rb', line 38

def paths(a, b)
  transitive_closure_class.linking(a, b)
end