Class: Delfos::FileSystem::Relation

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

Direct Known Subclasses

ChildFile

Defined Under Namespace

Classes: RelatedPaths

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_path, finish_path) ⇒ Relation

Returns a new instance of Relation.



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

def initialize(start_path, finish_path)
  @start_path = start_path
  @finish_path = finish_path
end

Instance Attribute Details

#finish_pathObject (readonly)

Returns the value of attribute finish_path.



5
6
7
# File 'lib/delfos/file_system/relation.rb', line 5

def finish_path
  @finish_path
end

#start_pathObject (readonly)

Returns the value of attribute start_path.



5
6
7
# File 'lib/delfos/file_system/relation.rb', line 5

def start_path
  @start_path
end

Instance Method Details

#distanceObject



20
21
22
23
24
25
# File 'lib/delfos/file_system/relation.rb', line 20

def distance
  return traversed_files.length       if both_files?
  return traversed_directories.length if both_directories?

  traversed_files.length + traversed_directories.length
end

#other_directoriesObject



16
17
18
# File 'lib/delfos/file_system/relation.rb', line 16

def other_directories
  RelatedPaths.new(start_path).directories
end

#other_filesObject



12
13
14
# File 'lib/delfos/file_system/relation.rb', line 12

def other_files
  RelatedPaths.new(start_path).files
end

#possible_lengthObject



27
28
29
# File 'lib/delfos/file_system/relation.rb', line 27

def possible_length
  other_files.length + other_directories.length
end

#subset_to_traverse(collection:, start:, finish:, start_at_end: true) ⇒ Object



49
50
51
52
53
# File 'lib/delfos/file_system/relation.rb', line 49

def subset_to_traverse(collection:, start:, finish:, start_at_end: true)
  start_index, finish_index = indexes_from(collection, start, finish, start_at_end)

  Array collection[start_index..finish_index]
end

#traversed_directoriesObject



40
41
42
43
44
45
46
47
# File 'lib/delfos/file_system/relation.rb', line 40

def traversed_directories
  start_at_end = (start_path.file? && finish_path.directory?) || (start_path.directory? && finish_path.file?)

  subset_to_traverse(collection: other_directories,
                     start: start_path,
                     finish: finish_path,
                     start_at_end: start_at_end)
end

#traversed_filesObject



31
32
33
34
35
36
37
38
# File 'lib/delfos/file_system/relation.rb', line 31

def traversed_files
  start_at_end = (start_path.file? && finish_path.directory?)

  subset_to_traverse(collection: other_files,
                     start: start_path,
                     finish: finish_path,
                     start_at_end: start_at_end)
end