Class: Delfos::FileSystem::DistanceCalculation

Inherits:
Object
  • Object
show all
Defined in:
lib/delfos/file_system/distance_calculation.rb

Defined Under Namespace

Classes: TraversalPathCalculator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_a, path_b) ⇒ DistanceCalculation

Returns a new instance of DistanceCalculation.



10
11
12
# File 'lib/delfos/file_system/distance_calculation.rb', line 10

def initialize(path_a, path_b)
  @path_a, @path_b = PathDetermination.for(path_a, path_b)
end

Instance Attribute Details

#path_aObject (readonly)

Returns the value of attribute path_a.



8
9
10
# File 'lib/delfos/file_system/distance_calculation.rb', line 8

def path_a
  @path_a
end

#path_bObject (readonly)

Returns the value of attribute path_b.



8
9
10
# File 'lib/delfos/file_system/distance_calculation.rb', line 8

def path_b
  @path_b
end

#traversal_aObject (readonly)

Returns the value of attribute traversal_a.



14
15
16
# File 'lib/delfos/file_system/distance_calculation.rb', line 14

def traversal_a
  @traversal_a
end

#traversal_bObject (readonly)

Returns the value of attribute traversal_b.



14
15
16
# File 'lib/delfos/file_system/distance_calculation.rb', line 14

def traversal_b
  @traversal_b
end

Instance Method Details

#klass_for(a, b) ⇒ Object



28
29
30
31
# File 'lib/delfos/file_system/distance_calculation.rb', line 28

def klass_for(a, b)
  return ChildFile if b + ".." == a
  Relation
end

#sibling_directories(path) ⇒ Object



41
42
43
# File 'lib/delfos/file_system/distance_calculation.rb', line 41

def sibling_directories(path)
  siblings(path).select { |f| File.directory?(f) }
end

#sum_possible_traversalsObject



37
38
39
# File 'lib/delfos/file_system/distance_calculation.rb', line 37

def sum_possible_traversals
  traversals.inject(0) { |a, e| a + e.possible_length }
end

#sum_traversalsObject



33
34
35
# File 'lib/delfos/file_system/distance_calculation.rb', line 33

def sum_traversals
  traversals.inject(0) { |a, e| a + e.distance }
end

#traversal_pathObject



45
46
47
# File 'lib/delfos/file_system/distance_calculation.rb', line 45

def traversal_path
  TraversalPathCalculator.new(path_a, path_b).path
end

#traversalsObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/delfos/file_system/distance_calculation.rb', line 16

def traversals
  result = []
  path = traversal_path

  path.each_cons(2) do |start, finish|
    klass = klass_for(start, finish)
    result.push(klass.new(start, finish))
  end

  result
end