class ZipContainer::ManagedFile

A ManagedFile is used to reserve a filename in a Container namespace.

Public Class Methods

new(name, required = false, validation_proc = nil) → ManagedFile click to toggle source

Create a new ManagedFile with the supplied name. Options that can be passed in are:

  • :required whether it is required to exist or not (default false).

  • :hidden whether it is hidden for normal operations.

  • :validation_proc should be a Proc that takes a single parameter, to which will be supplied the contents of the file, and returns true or false depending on whether the contents of the file were validated or not (default nil).

For more complex content validation subclasses may override the validate method.

The following example creates a ManagedFile that is not required to be present in the container, but if it is, its contents must be the single word “Boo!”.

valid = Proc.new { |contents| contents == "Boo!" }
ManagedFile.new("Surprize.txt", :required => false,
  :validation_proc => valid)
Calls superclass method ZipContainer::ManagedEntry.new
# File lib/zip-container/entries/file.rb, line 62
def initialize(name, options = {})
  options = {
    :required => false,
    :hidden => false,
    :validation_proc => nil
  }.merge(options)

  super(name, options[:required], options[:hidden])

  @validation_proc =
    options[:validation_proc].is_a?(Proc) ? options[:validation_proc] : nil
end

Public Instance Methods

verify → Array click to toggle source

Verify this ManagedFile for correctness. The contents are validated if required.

If it does not pass verification a list of reasons why it fails is returned. The empty list is returned if verification passes.

Calls superclass method ZipContainer::ManagedEntry#verify
# File lib/zip-container/entries/file.rb, line 83
def verify
  messages = super

  unless (exists? ? validate : true)
    messages << "The contents of file '#{full_name}' do not pass validation."
  end

  messages
end

Protected Instance Methods

validate → boolean click to toggle source

Validate the contents of this ManagedFile. By default this methods uses the validation Proc supplied on object initialization if there is one. If not it simply returns true (no validation was required).

For complex validations of content subclasses can override this method.

# File lib/zip-container/entries/file.rb, line 103
def validate
  @validation_proc.nil? ? true : @validation_proc.call(contents)
end