Changeset 206

Show
Ignore:
Timestamp:
06/07/05 05:08:27 (3 years ago)
Author:
ged
Message:

- Added Module#implements and #implements? aliases
- Added Class#alias_class_method

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/simplest-thing/lib/fm/utils.rb

    r172 r206  
    2020 
    2121require 'rbconfig' 
     22 
     23 
     24### A couple of syntactic sugar aliases for the Module class. 
     25### 
     26### [<tt>Module::implements</tt>] 
     27###     An alias for <tt>include</tt>. This allows syntax of the form: 
     28###       class MyClass < FaerieMUD::Object; implements Singleton 
     29###         ... 
     30###       end 
     31### 
     32### [<tt>Module::implements?</tt>] 
     33###     An alias for <tt>Module#<</tt>, which allows one to ask 
     34###     <tt>SomeClass.implements?( Singleton )</tt>. 
     35### 
     36class Module 
     37 
     38    # Syntactic sugar for mixin/interface modules.  (Borrowed from Hipster's 
     39    # component "conceptual script" - http://www.xs4all.nl/~hipster/) 
     40    alias :implements :include 
     41    alias :implements? :include? 
     42end 
     43 
     44 
     45### A couple of utility methods for the Class class. 
     46### [<tt>Class::alias_class_method</tt>] 
     47###     Create an alias for a class method. Borrowed from RubyTreasures by Paul 
     48###     Brannan <paul@nospam.atdesk.com>. 
     49class Class 
     50 
     51    ### Alias a class method. 
     52    def alias_class_method( newSym, oldSym ) 
     53        retval = nil 
     54        eval %{ 
     55          class << self 
     56            retval = alias_method :#{newSym}, :#{oldSym} 
     57          end 
     58        } 
     59        return retval 
     60    rescue Exception => err 
     61        # Mangle exceptions to point someplace useful 
     62        frames = err.backtrace 
     63        frames.shift while frames.first =~ /#{__FILE__}/ 
     64        Kernel::raise err, err.message.gsub(/in `\w+'/, "in `alias_class_method'"), frames 
     65    end 
     66end 
    2267 
    2368