Class: FilepathList

Inherits:
Object
  • Object
show all
Includes:
Enumerable, ArrayMethods, EntriesMethods
Defined in:
lib/filepath/filepathlist.rb

Defined Under Namespace

Modules: ArrayMethods, EntriesMethods

Constant Summary collapse

SEPARATOR =
':'.freeze

Instance Method Summary collapse

Methods included from EntriesMethods

#exclude, #map, #select

Constructor Details

#initialize(raw_entries = nil) ⇒ FilepathList

Returns a new instance of FilepathList.



9
10
11
12
# File 'lib/filepath/filepathlist.rb', line 9

def initialize(raw_entries = nil)
  raw_entries ||= []
  @entries = raw_entries.map { |e| e.as_path }
end

Instance Method Details

#*(other_list) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/filepath/filepathlist.rb', line 49

def *(other_list)
  if !other_list.is_a? FilepathList
    other_list = FilepathList.new(Array(other_list))
  end
  other_entries = other_list.entries
  paths = @entries.product(other_entries).map { |p1, p2| p1 / p2 }
  return FilepathList.new(paths)
end

#+(extra_entries) ⇒ Object



35
36
37
# File 'lib/filepath/filepathlist.rb', line 35

def +(extra_entries)
  return FilepathList.new(@entries + extra_entries.to_a)
end

#-(others) ⇒ Object



39
40
41
42
43
# File 'lib/filepath/filepathlist.rb', line 39

def -(others)
  remaining_entries = @entries - others.as_path_list.to_a

  return FilepathList.new(remaining_entries)
end

#/(extra_path) ⇒ Object



31
32
33
# File 'lib/filepath/filepathlist.rb', line 31

def /(extra_path)
  return self.map { |path| path / extra_path }
end

#<<(extra_path) ⇒ Object



45
46
47
# File 'lib/filepath/filepathlist.rb', line 45

def <<(extra_path)
  return FilepathList.new(@entries + [extra_path.as_path])
end

#==(other) ⇒ Object



101
102
103
# File 'lib/filepath/filepathlist.rb', line 101

def ==(other)
  @entries == other.as_path_list.to_a
end

#as_path_listFilepathList

Returns the path list itself.

Returns:



83
84
85
# File 'lib/filepath/filepathlist.rb', line 83

def as_path_list
  self
end

#directoriesObject



27
28
29
# File 'lib/filepath/filepathlist.rb', line 27

def directories
  return select_entries(:directory)
end

#filesObject



19
20
21
# File 'lib/filepath/filepathlist.rb', line 19

def files
  return select_entries(:file)
end


23
24
25
# File 'lib/filepath/filepathlist.rb', line 23

def links
  return select_entries(:link)
end

#remove_common_segmentsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/filepath/filepathlist.rb', line 58

def remove_common_segments
  all_segs = @entries.map(&:segments)
  max_length = all_segs.map(&:length).min

  idx_different = nil

  (0..max_length).each do |i|
    segment = all_segs.first[i]

    different = all_segs.any? { |segs| segs[i] != segment }
    if different
      idx_different = i
      break
    end
  end

  idx_different ||= max_length

  remaining_segs = all_segs.map { |segs| segs[idx_different..-1] }

  return FilepathList.new(remaining_segs)
end

#select_entries(type) ⇒ Object



14
15
16
17
# File 'lib/filepath/filepathlist.rb', line 14

def select_entries(type)
  raw_entries = @entries.delete_if { |e| !e.send(type.to_s + '?') }
  return FilepathList.new(raw_entries)
end

#to_aObject



87
88
89
# File 'lib/filepath/filepathlist.rb', line 87

def to_a
  @entries
end

#to_sObject



91
92
93
# File 'lib/filepath/filepathlist.rb', line 91

def to_s
  @to_s ||= @entries.map(&:to_str).join(SEPARATOR)
end