Class: FilePathList

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

Defined Under Namespace

Modules: ArrayMethods

Constant Summary collapse

SEPARATOR =
':'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(raw_entries = nil) ⇒ FilePathList

Returns a new instance of FilePathList.



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

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

Instance Method Details

#*(other_list) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/filepath/filepathlist.rb', line 68

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



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

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

#-(others) ⇒ Object



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

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

  return FilePathList.new(remaining_entries)
end

#/(extra_path) ⇒ Object



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

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

#<<(extra_path) ⇒ Object



64
65
66
# File 'lib/filepath/filepathlist.rb', line 64

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

#==(other) ⇒ Object



120
121
122
# File 'lib/filepath/filepathlist.rb', line 120

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

#as_path_listFilePathList

Returns the path list itself.

Returns:



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

def as_path_list
  self
end

#directoriesObject



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

def directories
  return select_entries(:directory)
end

#exclude(pattern = nil, &block) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/filepath/filepathlist.rb', line 46

def exclude(pattern = nil, &block)
  if block_given?
    select { |e| !block.call(e) }
  else
    select { |e| !(e =~ pattern) }
  end
end

#filesObject



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

def files
  return select_entries(:file)
end


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

def links
  return select_entries(:link)
end

#remove_common_segmentsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/filepath/filepathlist.rb', line 77

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(pattern = nil, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/filepath/filepathlist.rb', line 54

def select(pattern = nil, &block)
  if !block_given?
    block = proc { |e| e =~ pattern }
  end

  remaining_entries = @entries.select { |e| block.call(e) }

  return FilePathList.new(remaining_entries)
end

#select_entries(type) ⇒ Object



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

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

#to_aObject



106
107
108
# File 'lib/filepath/filepathlist.rb', line 106

def to_a
  @entries
end

#to_sObject



110
111
112
# File 'lib/filepath/filepathlist.rb', line 110

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