Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/case_check/core-ext.rb

Overview

Extend File with case-insensitive utility functions

Class Method Summary collapse

Class Method Details

.case_insensitive_canonical_name(name, base = nil) ⇒ Object

Finds the true filename for the file which can be accessed as “name” case-insensitively



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/case_check/core-ext.rb', line 15

def self.case_insensitive_canonical_name(name, base=nil)
  first, rest = File.expand_path(name, '/')[1, name.size].split('/', 2)
  base ||= ''
  actual_files = Dir[File.join(base, '*')].collect { |fn| fn[(base.length + 1) .. -1] }
  match =
    if actual_files.include?(first)
      first
    else
      actual_files.detect { |fn| fn.downcase == first.downcase }
    end
  if rest && match
    case_insensitive_canonical_name(rest, File.join(base, match))
  elsif match
    File.join(base, match)
  else
    nil
  end
end

.exists_exactly?(name, base = nil) ⇒ Boolean

Determines if the given filename maps exactly to an existing file, even if the underlying filesystem is case-insensitive

Returns:

  • (Boolean)


5
6
7
8
9
10
11
# File 'lib/case_check/core-ext.rb', line 5

def self.exists_exactly?(name, base=nil)
  first, rest = File.expand_path(name, '/')[1, name.size].split('/', 2)
  base ||= ''
  actual_files = Dir[File.join(base, '*')]
  candidate = File.join(base, first)
  actual_files.include?(candidate) && (!rest || self.exists_exactly?(rest, candidate))
end