Class: NWN::Resources::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/nwn/res.rb

Overview

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

Direct Known Subclasses

Erf::Erf, Key::Key, DirectoryContainer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContainer

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

#contentObject (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_filenameObject (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

#filenamesObject

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.

Returns:

  • (Boolean)


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