Class: ZipContainer::ManagedEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/zip-container/managed_entry.rb

Overview

ManagedEntry is the superclass of ManagedDirectory and ManagedFile. It should not be used directly but may be subclassed if necessary.

Direct Known Subclasses

ManagedDirectory, ManagedFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, required, hidden) ⇒ ManagedEntry

:call-seq:

new(name, required) -> ManagedEntry

Create a new ManagedEntry with the supplied name. The entry should also be marked as required or not and whether it is hidden for normal operations.



54
55
56
57
58
59
# File 'lib/zip-container/managed_entry.rb', line 54

def initialize(name, required, hidden)
  @parent = nil
  @name = name
  @required = required
  @hidden = hidden
end

Instance Attribute Details

#nameObject (readonly)

The name of the ManagedEntry. For the full path name of this entry use full_name.



42
43
44
# File 'lib/zip-container/managed_entry.rb', line 42

def name
  @name
end

#parent=(value) ⇒ Object (writeonly)

Allows the object in which this entry has been registered to tell it who it is.



46
47
48
# File 'lib/zip-container/managed_entry.rb', line 46

def parent=(value)
  @parent = value
end

Instance Method Details

#exists?Boolean

:call-seq:

exists? -> true or false

Does this ManagedEntry exist in the Container?

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
# File 'lib/zip-container/managed_entry.rb', line 99

def exists?
  container.entries.each do |entry|
    test = entry.ftype == :directory ? "#{full_name}/" : full_name
    return true if entry.name == test
  end

  false
end

#full_nameObject

:call-seq:

full_name -> string

The fully qualified name of this ManagedEntry.



65
66
67
68
69
70
71
# File 'lib/zip-container/managed_entry.rb', line 65

def full_name
  if @parent.is_a?(ZipContainer::Container)
    @name
  else
    "#{@parent.full_name}/#{@name}"
  end
end

#hidden?Boolean

:call-seq:

hidden? -> true or false

Is this ManagedEntry hidden for normal operations?

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
# File 'lib/zip-container/managed_entry.rb', line 86

def hidden?
  # An entry is hidden if its parent is hidden.
  if @parent.is_a?(ZipContainer::Container)
    @hidden
  else
    @hidden || @parent.hidden?
  end
end

#required?Boolean

:call-seq:

required? -> true or false

Is this ManagedEntry required to be present according to the specification of its Container?

Returns:

  • (Boolean)


78
79
80
# File 'lib/zip-container/managed_entry.rb', line 78

def required?
  @required
end

#verifyObject

:call-seq:

verify -> Array

Verify this ManagedEntry returning a list of reasons why it fails if it does so. The empty list is returned if verification passes.

Subclasses should override this method if they require more complex verification to be done.



116
117
118
119
120
121
122
# File 'lib/zip-container/managed_entry.rb', line 116

def verify
  if @required && !exists?
    ["Entry '#{full_name}' is required but missing."]
  else
    []
  end
end

#verify!Object

:call-seq:

verify!

Verify this ManagedEntry raising a MalformedContainerError if it fails.



139
140
141
142
# File 'lib/zip-container/managed_entry.rb', line 139

def verify!
  messages = verify
  raise MalformedContainerError, messages unless messages.empty?
end

#verify?Boolean

:call-seq:

verify? -> true or false

Verify this ManagedEntry by checking that it exists if it is required according to its Container specification and validating its contents if necessary.

Returns:

  • (Boolean)


130
131
132
# File 'lib/zip-container/managed_entry.rb', line 130

def verify?
  verify.empty?
end