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

hidden_directory?(entry) → boolean click to toggle source

Is the supplied entry/name a hidden directory?

# File lib/zip-container/entries/managed.rb, line 109
def hidden_directory?(entry)
  name = entry_name(entry)
  managed_directory?(name) ? all_managed_entries[name].hidden? : false
end
hidden_entry?(entry) → boolean click to toggle source

Is the supplied entry/name a hidden?

# File lib/zip-container/entries/managed.rb, line 127
def hidden_entry?(entry)
  hidden_directory?(entry) || hidden_file?(entry)
end
hidden_file?(entry) → boolean click to toggle source

Is the supplied entry/name a hidden file?

# File lib/zip-container/entries/managed.rb, line 118
def hidden_file?(entry)
  name = entry_name(entry)
  managed_file?(name) ? all_managed_entries[name].hidden? : false
end
managed_directories → Array click to toggle source

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
managed_directory?(entry) → boolean click to toggle source

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
managed_directory_names → Array click to toggle source

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
managed_entries → Array click to toggle source

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
managed_entry?(entry) → boolean click to toggle source

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
managed_entry_names → Array click to toggle source

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
managed_file?(entry) → boolean click to toggle source

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
managed_file_names → Array click to toggle source

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
managed_files → Array click to toggle source

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
verify_managed_entries → Array click to toggle source

All managed files and directories are checked to make sure that they exist and validate, if required.

# File lib/zip-container/entries/managed.rb, line 153
def verify_managed_entries
  messages = []

  @directories.each_value do |dir|
    messages += dir.verify
  end

  @files.each_value do |file|
    messages += file.verify
  end

  messages
end
verify_managed_entries! click to toggle source

All managed files and directories are checked to make sure that they exist and validate, if required.

# File lib/zip-container/entries/managed.rb, line 172
def verify_managed_entries!
  messages = verify_managed_entries
  raise MalformedContainerError.new(messages) unless messages.empty?
end

Protected Instance Methods

initialize_managed_entries click to toggle source
initialize_managed_entries(entry)
initialize_managed_entries(entries)

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 187
def initialize_managed_entries(entries = [])
  list = [*entries]
  @directories ||= {}
  @files ||= {}

  list.each { |item| register_managed_entry(item) }
end
register_managed_entry(entry) click to toggle source

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 206
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