Class: Synqa::LocalContentLocation

Inherits:
ContentLocation show all
Defined in:
lib/synqa.rb

Overview

A directory of files on a local system. The corresponding content tree can be calculated directly using Ruby library functions.

Instance Attribute Summary collapse

Attributes inherited from ContentLocation

#cachedContentFile

Instance Method Summary collapse

Methods inherited from ContentLocation

#clearCachedContentFile, #getCachedContentTree, #getCachedContentTreeMapOfHashes, #getExistingCachedContentTreeFile

Constructor Details

#initialize(baseDirectory, hashClass, cachedContentFile = nil) ⇒ LocalContentLocation

Returns a new instance of LocalContentLocation.



813
814
815
816
817
# File 'lib/synqa.rb', line 813

def initialize(baseDirectory, hashClass, cachedContentFile = nil)
  super(cachedContentFile)
  @baseDirectory = baseDirectory
  @hashClass = hashClass
end

Instance Attribute Details

#baseDirectoryObject (readonly)

the base directory, for example of type Based::BaseDirectory. Methods invoked are: allFiles, subDirs and fullPath. For file and dir objects returned by allFiles & subDirs, methods invoked are: relativePath and fullPath



809
810
811
# File 'lib/synqa.rb', line 809

def baseDirectory
  @baseDirectory
end

#hashClassObject (readonly)

the ruby class that generates the hash, e.g. Digest::SHA256



811
812
813
# File 'lib/synqa.rb', line 811

def hashClass
  @hashClass
end

Instance Method Details

#getContentTreeObject

get the content tree for this base directory by iterating over all sub-directories and files within the base directory (and excluding the excluded files) and calculating file hashes using the specified Ruby hash class If there is an existing cached content file, use that to get the hash values of files whose modification time is earlier than the time value for the cached content tree. Also, if a cached content file is specified, write the final content tree back out to the cached content file.



830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# File 'lib/synqa.rb', line 830

def getContentTree
  cachedTimeAndMapOfHashes = getCachedContentTreeMapOfHashes
  cachedTime = cachedTimeAndMapOfHashes[0]
  cachedMapOfHashes = cachedTimeAndMapOfHashes[1]
  contentTree = ContentTree.new()
  contentTree.time = Time.now.utc
  for subDir in @baseDirectory.subDirs
    contentTree.addDir(subDir.relativePath)
  end
  for file in @baseDirectory.allFiles
    cachedDigest = cachedMapOfHashes[file.relativePath]
    if cachedTime and cachedDigest and File.stat(file.fullPath).mtime < cachedTime
      digest = cachedDigest
    else
      digest = hashClass.file(file.fullPath).hexdigest
    end
    contentTree.addFile(file.relativePath, digest)
  end
  contentTree.sort!
  if cachedContentFile != nil
    contentTree.writeToFile(cachedContentFile)
  end
  return contentTree
end

#getFullPath(relativePath) ⇒ Object

get the full path of a relative path (i.e. of a file/directory within the base directory)



820
821
822
# File 'lib/synqa.rb', line 820

def getFullPath(relativePath)
  return @baseDirectory.fullPath + relativePath
end