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.



68
69
70
# File 'lib/nwn/res.rb', line 68

def initialize
  @content = []
end

Instance Attribute Details

#contentObject (readonly)

An array of all ContentObjects indexed by this Container.



66
67
68
# File 'lib/nwn/res.rb', line 66

def content
  @content
end

Instance Method Details

#add(o) ⇒ Object

Add a content object giving the ContentObject



85
86
87
# File 'lib/nwn/res.rb', line 85

def add o
  @content << o
end

#add_file(filename, io = nil) ⇒ Object

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



80
81
82
# File 'lib/nwn/res.rb', line 80

def add_file filename, io = nil
  @content << ContentObject.new_from(filename, io)
end

#filenamesObject

Returns a list of filenames, all lowercase.



90
91
92
# File 'lib/nwn/res.rb', line 90

def filenames
  @content.map {|x| x.filename.downcase }
end

#get(filename) ⇒ Object

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



107
108
109
# File 'lib/nwn/res.rb', line 107

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.

Raises:

  • (Errno::ENOENT)


96
97
98
99
100
101
102
103
# File 'lib/nwn/res.rb', line 96

def get_content_object filename
  filename = filename.downcase
  ret = @content.select {|x| filename == x.filename }
  raise Errno::ENOENT,
    "No ContentObject with the given filename #{filename.inspect} found." if
      ret.size == 0
  ret[0]
end

#has?(filename) ⇒ Boolean

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

Returns:

  • (Boolean)


74
75
76
# File 'lib/nwn/res.rb', line 74

def has?(filename)
  filenames.index(filename.downcase) != nil
end