Class: NWN::Resources::Container
- Inherits:
-
Object
- Object
- NWN::Resources::Container
- Defined in:
- lib/nwn/res.rb
Overview
Wraps n ContentObjects; a baseclass for erf/key encapsulation.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
An array of all ContentObjects indexed by this Container.
-
#content_by_filename ⇒ Object
readonly
A hash containing filename.downcase => ContentObject.
Instance Method Summary collapse
-
#add(o) ⇒ Object
Add a content object giving the ContentObject.
-
#add_file(filename, io = nil) ⇒ Object
Add a content object giving a
filename
and a optionalio
. -
#filenames ⇒ Object
Returns a list of filenames, all lowercase.
-
#get(filename) ⇒ Object
Get the contents of the given filename.
-
#get_content_object(filename) ⇒ Object
Get the ContentObject pointing to the given filename.
-
#has?(filename) ⇒ Boolean
Returns true if the given filename is contained herein.
-
#initialize ⇒ Container
constructor
A new instance of Container.
- #remove(o) ⇒ Object
-
#remove_file(filename) ⇒ Object
Removes a content object by filename.
Constructor Details
#initialize ⇒ Container
Returns a new instance of Container.
73 74 75 76 |
# File 'lib/nwn/res.rb', line 73 def initialize @content = [] @content_by_filename = {} end |
Instance Attribute Details
#content ⇒ Object (readonly)
An array of all ContentObjects indexed by this Container. Do not modify, use add/remove instead.
67 68 69 |
# File 'lib/nwn/res.rb', line 67 def content @content end |
#content_by_filename ⇒ Object (readonly)
A hash containing filename.downcase => ContentObject. Do not modify, use add/remove instead.
71 72 73 |
# File 'lib/nwn/res.rb', line 71 def content_by_filename @content_by_filename end |
Instance Method Details
#add(o) ⇒ Object
Add a content object giving the ContentObject
91 92 93 94 95 |
# File 'lib/nwn/res.rb', line 91 def add o @content << o @content_by_filename[o.filename.downcase] = o @filenames = nil end |
#add_file(filename, io = nil) ⇒ Object
Add a content object giving a filename
and a optional io
.
86 87 88 |
# File 'lib/nwn/res.rb', line 86 def add_file filename, io = nil add ContentObject.new_from(filename, io) end |
#filenames ⇒ Object
Returns a list of filenames, all lowercase.
113 114 115 |
# File 'lib/nwn/res.rb', line 113 def filenames @filenames ||= @content_by_filename.keys end |
#get(filename) ⇒ Object
Get the contents of the given filename. Raises ENOENT if not mapped.
126 127 128 |
# File 'lib/nwn/res.rb', line 126 def get filename get_content_object(filename).get end |
#get_content_object(filename) ⇒ Object
Get the ContentObject pointing to the given filename. Raises ENOENT if not mapped.
119 120 121 122 |
# File 'lib/nwn/res.rb', line 119 def get_content_object filename @content_by_filename[filename.downcase] or raise Errno::ENOENT, "No ContentObject with the given filename #{filename.inspect} found." end |
#has?(filename) ⇒ Boolean
Returns true if the given filename is contained herein. Case-insensitive.
80 81 82 |
# File 'lib/nwn/res.rb', line 80 def has?(filename) @content_by_filename[filename.downcase] != nil end |
#remove(o) ⇒ Object
106 107 108 109 110 |
# File 'lib/nwn/res.rb', line 106 def remove o @content.delete(o) @content_by_filename.delete(o.filename) @filenames = nil end |
#remove_file(filename) ⇒ Object
Removes a content object by filename. Raises ENOENT if no object by that name is contained.
99 100 101 102 103 104 |
# File 'lib/nwn/res.rb', line 99 def remove_file filename @content_by_filename[filename.downcase] or raise Errno::ENOENT, "No ContentObject with the given filename #{filename.inspect} found." remove @content_by_filename[filename.downcase] end |