module ZipContainer::ManagedEntries
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.
Public Instance Methods
Return the list of managed directories.
# File lib/zip-container/entries/managed.rb, line 48 def managed_directories return @managed_directories if @managed_directories dirs = @directories.values @managed_directories = dirs + dirs.map { |d| d.managed_directories }.flatten end
Is the supplied entry/name a managed directory?
# File lib/zip-container/entries/managed.rb, line 68 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 60 def managed_directory_names @managed_directory_names ||= managed_directories.map { |d| d.full_name } end
Return the list of managed files and directories.
# File lib/zip-container/entries/managed.rb, line 76 def managed_entries @managed_entries ||= managed_files + managed_directories end
Is the supplied entry/name a managed entry?
# File lib/zip-container/entries/managed.rb, line 92 def managed_entry?(entry, list = managed_entry_names) name = entry_name(entry) 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 84 def managed_entry_names @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 101 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 144 def managed_file_names @managed_file_names ||= managed_files.map { |f| f.full_name } end
Return the list of managed files.
# File lib/zip-container/entries/managed.rb, line 135 def managed_files @managed_files ||= @files.values + @directories.values.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 153 def verify_managed_entries! @directories.each_value do |dir| dir.verify! end @files.each_value do |file| file.verify! end true end
Protected Instance Methods
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 175 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 194 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