Class: ZipContainer::ManagedEntry

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/zip-container/entries/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

Methods included from Util

#entry_name

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.



51
52
53
54
55
56
# File 'lib/zip-container/entries/entry.rb', line 51

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.



43
44
45
# File 'lib/zip-container/entries/entry.rb', line 43

def name
  @name
end

Instance Method Details

#exists?Boolean

:call-seq:

exists? -> true or false

Does this ManagedEntry exist in the Container?

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/zip-container/entries/entry.rb', line 88

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.



62
63
64
# File 'lib/zip-container/entries/entry.rb', line 62

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

#hidden?Boolean

:call-seq:

hidden? -> true or false

Is this ManagedEntry hidden for normal operations?

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/zip-container/entries/entry.rb', line 79

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

#parent=(parent) ⇒ Object

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



100
101
102
# File 'lib/zip-container/entries/entry.rb', line 100

def parent=(parent)
  @parent = parent
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)


71
72
73
# File 'lib/zip-container/entries/entry.rb', line 71

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.



113
114
115
116
117
118
119
# File 'lib/zip-container/entries/entry.rb', line 113

def verify
  unless !@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.



136
137
138
139
# File 'lib/zip-container/entries/entry.rb', line 136

def verify!
  messages = verify
  raise MalformedContainerError.new(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)


127
128
129
# File 'lib/zip-container/entries/entry.rb', line 127

def verify?
  verify.empty?
end