Module: Cocos::Find

Included in:
Kernel
Defined in:
lib/cocos/find_file.rb

Instance Method Summary collapse

Instance Method Details

#find_dir(name, path: []) ⇒ Object



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

def find_dir( name, path: [] )
    dirpath = File.expand_path( name )
    return dirpath   if Dir.exist?( dirpath )

##  note - if name starts with / or \ assume it's absolute!!
##    do NOT search!!!
##     note - search still works for
##               ./austria  or ../austria or such
##
##  todo/check/fix-fix-fix
##     is there a File.absolute? or such method for reuse??
##
##  todo/fix-fix-fix add absolute check upstream to find_file too!!!
   ## return nil    if name.start_with?( %r{[/\\]} )
   ##  note - Pathname#absolute? is basically !Pathname#relative? !!!
      return nil    if Pathname.new(name).absolute?

    path.each do |basedir|
        ## todo/check -  always make sure basedir is an absolute/expanded path - why? why not?
        dirpath = File.expand_path( name, basedir )
        return dirpath   if Dir.exist?( dirpath )
    end

    nil   ## return nil if not found
end

#find_file(name, path:) ⇒ Object

note - always expand path for now and return absolute path!

note - do NOT search path if name passed in is absolute!!!



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cocos/find_file.rb', line 36

def find_file( name, path: )
    filepath = File.expand_path( name )
    return filepath   if File.file?( filepath )

##  note - if name starts with root / or c:\
##          assume it's absolute - handle by Pathname lib for now
##    do NOT search!!!
    return nil    if Pathname.new(name).absolute?

    path.each do |basedir|
        filepath = File.expand_path( name, basedir )
        return filepath   if File.file?( filepath )
    end

    nil   ## return nil if not found
end

#find_file!(name, path:) ⇒ Object

Raises:

  • (Errno::ENOENT)


18
19
20
21
22
# File 'lib/cocos/find_file.rb', line 18

def find_file!( name, path: )
    filepath = find_file( name, path: path )
    raise Errno::ENOENT, "file #{name.inspect} not found; looking in path #{path.inspect}"   if filepath.nil?
    filepath
end