This module provides support for managed file and directory entries.
Note! If you mix this module in you must
call initialize_managed_entries
in your constructor to ensure
that the internal lists of managed entries are correctly assigned.
Return the list of managed directories.
# File lib/zip-container/entries/managed.rb, line 47 def managed_directories @directories.values end
Is the supplied entry/name a managed directory?
# File lib/zip-container/entries/managed.rb, line 63 def managed_directory?(entry) managed_entry?(entry, managed_directory_names) end
Return the list of managed directory names.
# File lib/zip-container/entries/managed.rb, line 55 def managed_directory_names expand_names(@directories.keys) end
Return the list of managed files and directories.
# File lib/zip-container/entries/managed.rb, line 71 def managed_entries managed_files + managed_directories end
Is the supplied entry/name a managed entry?
# File lib/zip-container/entries/managed.rb, line 87 def managed_entry?(entry, list = managed_entry_names) name = entry.kind_of?(::Zip::Entry) ? entry.name : entry name.chop! if name.end_with? "/" list.map { |n| n.downcase }.include? name.downcase end
Return the list of managed file and directory names.
# File lib/zip-container/entries/managed.rb, line 79 def managed_entry_names managed_file_names + managed_directory_names end
Is the supplied entry/name a managed file?
# File lib/zip-container/entries/managed.rb, line 97 def managed_file?(entry) managed_entry?(entry, managed_file_names) end
Return the list of managed file names.
# File lib/zip-container/entries/managed.rb, line 113 def managed_file_names expand_names(@files.keys) + managed_directories.map { |d| d.managed_file_names }.flatten end
Return the list of managed files.
# File lib/zip-container/entries/managed.rb, line 105 def managed_files @files.values + managed_directories.map { |d| d.managed_files }.flatten end
All managed files and directories are checked to make sure that they exist, if required.
# File lib/zip-container/entries/managed.rb, line 123 def verify_managed_entries! @directories.each_value do |dir| dir.verify! end @files.each_value do |file| file.verify! end true end
Initialize the managed entries and register any that are supplied. A single ManagedFile or ManagedDirectory or a list of them can be provided.
# File lib/zip-container/entries/managed.rb, line 145 def initialize_managed_entries(entries = []) list = [*entries] @directories ||= {} @files ||= {} list.each { |item| register_managed_entry(item) } end
Register a ManagedFile or ManagedDirectory.
A ManagedFile is used to reserve the name of a file in the container namespace and can describe how to verify the contents of it if required.
A ManagedDirectory is used to both reserve the name of a directory in the container namespace and act as an interface to the (possibly) managed files within it.
# File lib/zip-container/entries/managed.rb, line 164 def register_managed_entry(entry) unless entry.is_a?(ManagedDirectory) || entry.is_a?(ManagedFile) raise ArgumentError.new("The supplied entry must be of type " "ManagedDirectory or ManagedFile or a subclass of either.") end entry.parent = self @directories[entry.name] = entry if entry.is_a? ManagedDirectory @files[entry.name] = entry if entry.is_a? ManagedFile end