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.



775
776
777
778
779
# File 'lib/synqa.rb', line 775

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



771
772
773
# File 'lib/synqa.rb', line 771

def baseDirectory
  @baseDirectory
end

#hashClassObject (readonly)

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



773
774
775
# File 'lib/synqa.rb', line 773

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.



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/synqa.rb', line 792

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)



782
783
784
# File 'lib/synqa.rb', line 782

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