Class: Longleaf::StorageLocation

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/models/storage_location.rb

Overview

Representation of a configured storage location

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, path:, metadata_path:, metadata_digests: []) ⇒ StorageLocation

Returns a new instance of StorageLocation.

Parameters:

  • name (String)

    the name of this storage location

  • path (String)

    absolute path where the storage location is located

  • metadata_path (String)

    absolute path where the metadata for files in this location will be stored.

  • metadata_digests (defaults to: [])

    list of digest algorithms to use for metadata file digests in this location.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/longleaf/models/storage_location.rb', line 15

def initialize(name:, path:, metadata_path:, metadata_digests: [])
  raise ArgumentError.new("Parameters name, path and metadata_path are required") unless name && path && 

  @path = path
  @path += '/' unless @path.end_with?('/')
  @name = name
  @metadata_path = 
  @metadata_path += '/' unless @metadata_path.end_with?('/')

  if .nil?
    @metadata_digests = []
  elsif .is_a?(String)
    @metadata_digests = [.downcase]
  else
    @metadata_digests = .map(&:downcase)
  end
  DigestHelper::validate_algorithms(@metadata_digests)
end

Instance Attribute Details

#metadata_digestsObject (readonly)

Returns the value of attribute metadata_digests.



9
10
11
# File 'lib/longleaf/models/storage_location.rb', line 9

def 
  @metadata_digests
end

#metadata_pathObject (readonly)

Returns the value of attribute metadata_path.



8
9
10
# File 'lib/longleaf/models/storage_location.rb', line 8

def 
  @metadata_path
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/longleaf/models/storage_location.rb', line 6

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/longleaf/models/storage_location.rb', line 7

def path
  @path
end

Instance Method Details

#available?Boolean

Checks that the path and metadata path defined in this location are available

Returns:

  • (Boolean)

Raises:



66
67
68
69
70
71
# File 'lib/longleaf/models/storage_location.rb', line 66

def available?
  raise StorageLocationUnavailableError.new("Path does not exist or is not a directory: #{@path}")\
      unless Dir.exist?(@path)
  raise StorageLocationUnavailableError.new("Metadata path does not exist or is not a directory: #{@metadata_path}")\
      unless Dir.exist?(@metadata_path)
end

#get_metadata_path_for(file_path) ⇒ Object

Get the path for the metadata file for the given file path located in this storage location.

Parameters:

  • file_path (String)

    path of the file

Raises:

  • (ArgumentError)

    if the file_path is not provided or is not in this storage location.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/longleaf/models/storage_location.rb', line 37

def (file_path)
  raise ArgumentError.new("A file_path parameter is required") if file_path.nil? || file_path.empty?
  raise ArgumentError.new("Provided file path is not contained by storage location #{@name}: #{file_path}") \
      unless file_path.start_with?(@path)

  md_path = file_path.sub(/^#{@path}/, @metadata_path)
  # If the file_path is to a file, then add metadata suffix.
  if md_path.end_with?('/')
    md_path
  else
    md_path + MetadataSerializer::
  end
end

#get_path_from_metadata_path(md_path) ⇒ String

Get the metadata path for the provided file path located in this storage location.

Parameters:

  • md_path (String)

    metadata file path

Returns:

  • (String)

    the path for the file associated with this metadata

Raises:

  • (ArgumentError)

    if the md_path is not in this storage location



55
56
57
58
59
60
61
62
# File 'lib/longleaf/models/storage_location.rb', line 55

def (md_path)
  raise ArgumentError.new("A file_path parameter is required") if md_path.nil? || md_path.empty?
  raise ArgumentError.new("Provided metadata path is not contained by storage location #{@name}: #{md_path}") \
      unless md_path&.start_with?(@metadata_path)

  file_path = md_path.sub(/^#{@metadata_path}/, @path)
  file_path.sub(/#{MetadataSerializer::}$/, '')
end