### Home Classes Methods

### In Files

- lib/rubygame/named_resource.rb

### Methods

- #[]
- #[]=
- #autoload
- #basename
- #exist?
- #find_file

### Files

- CREDITS
- LICENSE
- NEWS
- README
- ROADMAP
- custom_sdl_load_paths.rdoc
- getting_started.rdoc
- keyboard_symbols.rdoc
- macosx_install.rdoc
- managing_framerate.rdoc
- surface_palettes.rdoc
- windows_install.rdoc

### Class/Module Index

Quicksearch

- Rubygame
- Rubygame::ActiveEvent
- Rubygame::Clock
- Rubygame::Color
- Rubygame::Color::ColorBase
- Rubygame::Color::ColorHSL
- Rubygame::Color::ColorHSV
- Rubygame::Color::ColorRGB
- Rubygame::Color::Palette
- Rubygame::Event
- Rubygame::EventActions
- Rubygame::EventActions::BlockAction
- Rubygame::EventActions::MethodAction
- Rubygame::EventActions::MultiAction
- Rubygame::EventHandler
- Rubygame::EventHandler::HasEventHandler
- Rubygame::EventHook
- Rubygame::EventQueue
- Rubygame::EventTriggers
- Rubygame::EventTriggers::AndTrigger
- Rubygame::EventTriggers::AttrTrigger
- Rubygame::EventTriggers::BlockTrigger
- Rubygame::EventTriggers::InstanceOfTrigger
- Rubygame::EventTriggers::KeyPressTrigger
- Rubygame::EventTriggers::KeyReleaseTrigger
- Rubygame::EventTriggers::KindOfTrigger
- Rubygame::EventTriggers::MouseMoveTrigger
- Rubygame::EventTriggers::MousePressTrigger
- Rubygame::EventTriggers::MouseReleaseTrigger
- Rubygame::EventTriggers::OrTrigger
- Rubygame::EventTriggers::TickTrigger
- Rubygame::EventTriggers::YesTrigger
- Rubygame::Events
- Rubygame::Events::ClockTicked
- Rubygame::Events::InputFocusGained
- Rubygame::Events::InputFocusLost
- Rubygame::Events::JoystickAxisMoved
- Rubygame::Events::JoystickBallMoved
- Rubygame::Events::JoystickButtonEvent
- Rubygame::Events::JoystickButtonPressed
- Rubygame::Events::JoystickButtonReleased
- Rubygame::Events::JoystickHatMoved
- Rubygame::Events::KeyPressed
- Rubygame::Events::KeyReleased
- Rubygame::Events::KeyboardEvent
- Rubygame::Events::MouseButtonEvent
- Rubygame::Events::MouseFocusGained
- Rubygame::Events::MouseFocusLost
- Rubygame::Events::MouseMoved
- Rubygame::Events::MousePressed
- Rubygame::Events::MouseReleased
- Rubygame::Events::QuitRequested
- Rubygame::Events::WindowExposed
- Rubygame::Events::WindowMinimized
- Rubygame::Events::WindowResized
- Rubygame::Events::WindowUnminimized
- Rubygame::ExposeEvent
- Rubygame::Ftor
- Rubygame::GL
- Rubygame::Hotspot
- Rubygame::JoyAxisEvent
- Rubygame::JoyBallEvent
- Rubygame::JoyDownEvent
- Rubygame::JoyHatEvent
- Rubygame::JoyUpEvent
- Rubygame::Joystick
- Rubygame::Key
- Rubygame::KeyDownEvent
- Rubygame::KeyUpEvent
- Rubygame::MailQueue
- Rubygame::MediaBag
- Rubygame::Mixer
- Rubygame::Mixer::Music
- Rubygame::Mixer::Sample
- Rubygame::Mouse
- Rubygame::MouseDownEvent
- Rubygame::MouseMotionEvent
- Rubygame::MouseUpEvent
- Rubygame::Music
- Rubygame::NamedResource
- Rubygame::NamedResource::NamedResourceClassMethods
- Rubygame::QuitEvent
- Rubygame::Rect
- Rubygame::ResizeEvent
- Rubygame::SDLError
- Rubygame::SFont
- Rubygame::Screen
- Rubygame::Sound
- Rubygame::Sprites
- Rubygame::Sprites::DepthSortGroup
- Rubygame::Sprites::Group
- Rubygame::Sprites::LimitGroup
- Rubygame::Sprites::Sprite
- Rubygame::Sprites::UpdateGroup
- Rubygame::Surface
- Rubygame::TTF

No matching classes.

# Rubygame::NamedResource::NamedResourceClassMethods

Adds class methods when the NamedResource module is included in a class. (Here, we are assuming that the NamedResource module was included in a class called MyClass.)

### Attributes

autoload_dirs[RW]

An Array of paths to check for files. See find_file.

### Public Instance Methods

MyClass[ name ] → instance or nil click to toggle source

Retrieves an instance of the class from a per-class resource table (Hash).

If no object has been saved under the given name, invoke autoload to try to load a new instance, store it in the Hash table under this name, and sets the instance's @name to this name.

    # File lib/rubygame/named_resource.rb, line 101
    def []( name )
      result = @resources[name]

      if result.nil?
        result = autoload(name)
        if result
          self[name] = result
          result.name = name
        end
      end

      return result
    end

MyClass[ name ] = instance click to toggle source

Stores an instance of the class in a per-class resource table (Hash) for future access. If another object is already stored with this name, the old record is lost.

May raise: TypeError, if you try to store anything

    that is not kind of this class.

    # File lib/rubygame/named_resource.rb, line 126
    def []=( name, value )
      if( value.kind_of? self )
        @resources[name] = value
      else
        raise TypeError, "#{self}#[]= can only store instances of #{self}"
      end
    end

autoload( name ) → instance or nil click to toggle source

This method is invoked when a non-existing resource is accessed with #[]. By default, this method simply returns nil, effectively disabling autoloading.

You should override this method in your class to provide class-specific loading behavior, or leave it as the default if you don't need autoloading. Your method should return either an instance of the class, or nil.

NOTE: The find_file method is useful for getting the full path to a file which matches the name. That's what it's there for, so you should use it!

    # File lib/rubygame/named_resource.rb, line 150
    def autoload( name )
      nil
    end

basename( path ) → filename click to toggle source

Returns the basename for the path (i.e. the filename without the directory). Same as File.basename

    # File lib/rubygame/named_resource.rb, line 162
    def basename( path )
      File.basename( path )
    end

exist?( path ) → true or false click to toggle source

True if the given path points to a file that exists, otherwise false. Same as File.exist?

    # File lib/rubygame/named_resource.rb, line 174
    def exist?( path )
      File.exist?(path)
    end

find_file( filename ) → path or nil click to toggle source

Checks every directory in @autoload_dirs for a file with the given name, and returns the path (directory and name) for the first match.

If no directories have a file with that name, return nil.

    # File lib/rubygame/named_resource.rb, line 189
    def find_file( filename )
      dir = @autoload_dirs.find { |dir|
        exist?( File.join(dir,filename) )
      }

      if dir
        return File.join(dir,filename)
      else
        return nil
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.