Class: ZipContainer::ManagedFile
- Inherits:
-
ManagedEntry
- Object
- ManagedEntry
- ZipContainer::ManagedFile
- 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
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ ManagedFile
constructor
:call-seq: new(name, required = false, validation_proc = nil) -> ManagedFile.
-
#verify ⇒ Object
:call-seq: verify -> Array.
Methods inherited from ManagedEntry
#exists?, #full_name, #hidden?, #required?, #verify!, #verify?
Methods included from Util
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 returnstrue
orfalse
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, = {}) = { required: false, hidden: false, validation_proc: nil }.merge() super(name, [:required], [:hidden]) @validation_proc = [:validation_proc].is_a?(Proc) ? [:validation_proc] : nil end |
Instance Method Details
#verify ⇒ Object
: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 = super valid = exists? ? validate : true unless valid << "The contents of file '#{full_name}' do not pass validation." end end |