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



1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
# File 'lib/tenjin.rb', line 1250

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



1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
# File 'lib/tenjin.rb', line 1272

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



1267
1268
1269
1270
# File 'lib/tenjin.rb', line 1267

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