A ManagedFile is used to reserve a filename in a Container namespace.
Create a new ManagedFile with the supplied name and whether it is required to exist or not.
If supplied validation_proc
should be a Proc that takes a
single parameter and returns true
or false
depending on whether the contents of the file were validated or not.
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", false, valid)
# File lib/zip-container/entries/file.rb, line 58 def initialize(name, required = false, validation_proc = nil) super(name, required) @validation_proc = validation_proc.is_a?(Proc) ? validation_proc : nil end
Verify this ManagedFile for correctness. The contents are validated if required.
A MalformedContainerError is raised if it does not pass verification.
# File lib/zip-container/entries/file.rb, line 71 def verify! super unless (exists? ? validate : true) raise MalformedContainerError.new("The contents of file " "'#{full_name}' do not pass validation.") end end
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 89 def validate @validation_proc.nil? ? true : @validation_proc.call(contents) end