Class: Tenjin::FileFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/tenjin.rb

Overview

helper class for Engine to find and read files

Instance Method Summary collapse

Instance Method Details

#find(filename, dirs = nil) ⇒ Object



1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
# File 'lib/tenjin.rb', line 1261

def find(filename, dirs=nil)
  if dirs
    #: if dirs specified then find file from it.
    for dir in dirs
      filepath = File.join(dir, filename)
      return filepath if File.file?(filepath)
    end
    #found = dirs.find {|dir| File.isfile(File.join(dir, filename)) }
    #return File.join(found, filename) if found
  else
    #: if dirs not specified then return filename if it exists.
    return filename if File.file?(filename)
  end
  #: if file not found then return nil.
  return nil
end

#read(filepath) ⇒ Object



1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
# File 'lib/tenjin.rb', line 1283

def read(filepath)
  begin
    #: if file exists then return file content and mtime.
    mtime = File.mtime(filepath)
    input = File.open(filepath, 'rb') {|f| f.read }
    mtime2 = File.mtime(filepath)
    if mtime != mtime2
      mtime = mtime2
      input = File.open(filepath, 'rb') {|f| f.read }
      mtime2 = File.mtime(filepath)
      if mtime != mtime2
        Tenjin.logger.warn("[tenjin.rb:#{__LINE__}] #{self.class.name}#read(): timestamp is changed while reading file.") if Tenjin.logger
      end
    end
    return input, mtime
  rescue Errno::ENOENT
    #: if file not found then return nil.
    return nil
  end
end

#timestamp(filepath) ⇒ Object



1278
1279
1280
1281
# File 'lib/tenjin.rb', line 1278

def timestamp(filepath)
  #: return mtime of filepath.
  return File.mtime(filepath)
end