class NWN::Resources::Container

Wraps n ContentObjects; a baseclass for erf/key encapsulation.

Attributes

content[R]

An array of all ContentObjects indexed by this Container. Do not modify, use add/remove instead.

content_by_filename[R]

A hash containing filename.downcase => ContentObject. Do not modify, use add/remove instead.

Public Class Methods

new() click to toggle source
# File lib/nwn/res.rb, line 73
def initialize
  @content = []
  @content_by_filename = {}
end

Public Instance Methods

add(o) click to toggle source

Add a content object giving the ContentObject

# 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) click to toggle source

Add a content object giving a filename and a optional io.

# File lib/nwn/res.rb, line 86
def add_file filename, io = nil
  add ContentObject.new_from(filename, io)
end
filenames() click to toggle source

Returns a list of filenames, all lowercase.

# File lib/nwn/res.rb, line 113
def filenames
  @filenames ||= @content_by_filename.keys
end
get(filename) click to toggle source

Get the contents of the given filename. Raises ENOENT if not mapped.

# File lib/nwn/res.rb, line 126
def get filename
  get_content_object(filename).get
end
get_content_object(filename) click to toggle source

Get the ContentObject pointing to the given filename. Raises ENOENT if not mapped.

# 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) click to toggle source

Returns true if the given filename is contained herein. Case-insensitive.

# File lib/nwn/res.rb, line 80
def has?(filename)
  @content_by_filename[filename.downcase] != nil
end
remove(o) click to toggle source
# 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) click to toggle source

Removes a content object by filename. Raises ENOENT if no object by that name is contained.

# 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