Module: Chingu::NamedResource::NamedResourceClassMethods

Defined in:
lib/chingu/named_resource.rb

Overview

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.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#autoload_dirsObject

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



87
88
89
# File 'lib/chingu/named_resource.rb', line 87

def autoload_dirs
  @autoload_dirs
end

Instance Method Details

#[](name) ⇒ Object

call-seq:

  MyClass[ name ]  ->  instance or nil

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.


105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/chingu/named_resource.rb', line 105

def []( name )
  result = @resources[name]

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

  return result
end

#[]=(name, value) ⇒ Object

call-seq:

  MyClass[ name ] = instance

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.


130
131
132
133
134
135
136
# File 'lib/chingu/named_resource.rb', line 130

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) ⇒ Object

call-seq:

  MyClass.autoload( name )  ->  instance or nil

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!


154
155
156
# File 'lib/chingu/named_resource.rb', line 154

def autoload( name )
  nil
end

#basename(path) ⇒ Object

call-seq:

  MyClass.basename( path )  ->  filename

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


166
167
168
# File 'lib/chingu/named_resource.rb', line 166

def basename( path )
  File.basename( path )
end

#clearObject

Clear all the cached assets



90
91
92
# File 'lib/chingu/named_resource.rb', line 90

def clear
  @resources.clear
end

#exist?(path) ⇒ Boolean

call-seq:

  MyClass.exist?( path )  ->  true or false

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


178
179
180
# File 'lib/chingu/named_resource.rb', line 178

def exist?( path )
  File.exist?(path)
end

#find_file(filename) ⇒ Object

call-seq:

  MyClass.find_file( filename )  ->  path or nil

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.


193
194
195
196
197
198
199
200
201
202
203
# File 'lib/chingu/named_resource.rb', line 193

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