Class: ZipContainer::ManagedFile

Inherits:
ManagedEntry show all
Defined in:
lib/zip-container/entries/file.rb

Overview

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

Instance Attribute Summary

Attributes inherited from ManagedEntry

#name, #parent

Instance Method Summary collapse

Methods inherited from ManagedEntry

#exists?, #full_name, #hidden?, #required?, #verify!, #verify?

Methods included from Util

#entry_name

Constructor Details

#initialize(name, options = {}) ⇒ ManagedFile

:call-seq:

new(name, required = false, validation_proc = nil) -> ManagedFile

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)


62
63
64
65
66
67
68
69
70
71
72
73
# 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

Instance Method Details

#verifyObject

:call-seq:

verify -> Array

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.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zip-container/entries/file.rb', line 83

def verify
  messages = super

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

  messages
end