Class Module
In: lib/facet/module/redefine_method.rb
lib/facet/module/initializer.rb
lib/facet/module/attr_reader.rb
lib/facet/module/methods.rb
lib/facet/module/attr_writer.rb
lib/facet/module/basename.rb
lib/facet/module/redirect.rb
lib/facet/module/attr_accessor.rb
lib/facet/module/alias_module_function.rb
lib/facet/module/abstract.rb
lib/facet/module/wrap_method.rb
lib/facet/module/include_as.rb
lib/facet/module/sort_attributes.rb
lib/facet/module/key_attributes.rb
lib/facet/module/attr_setter.rb
lib/facet/module/attr.rb
lib/facet/module/attr_checker.rb
lib/facet/module/redef.rb
lib/facet/module/by_name.rb
lib/facet/module/memoize.rb
lib/facet/module/wrap.rb
Parent: Object

Creates a method that requires to be overridding. If it not overridden and called upon a TypeError will be raised.

 require 'facet/module/abstract'

 class C
   abstract :a
 end

 c = C.new
 c.a  #=> Error: undefined abstraction #a

Methods

Constants

METHODS_SYMBOLS = [ :inherited, :ancestors, :local, :no_acestors, :public, :private, :protected ]

Public Instance methods

Shorthand for creating attributes.

  require 'facet/module/attr'

  attr :r, :w=, :a, :a=

is equivalent to

  attr_reader :r
  attr_writer :w
  attr_accessor :a

A "tester" form is also supported:

  attr :q?

which translates to

  def q?
    @q ? true : @q
  end

(see attr_tester)

Compatability is maintained with the old version of attr. So it is still possible to write:

  attr :a, true

It has been taken expanded to allow multiple names as well:

  attr :a, :b, :c, true

Modifies attr_accessor to return an array of the names of the methods it has defined.

Create an attribute method, _var?_, that returns a boolean test of on the instance variable. The result will always be either true, false or nil.

  require 'facet/module/attr_checker'

  attr_checker :a

is equivalent to

  def a?
    @a ? true : @a
  end

Modifies attr_reader to return an array of the names of the methods it has defined.

Create an attribute method for both getting and setting an instance variable.

  require 'facet/module/attr_setter'

  attr_setter :a

_is equivalent to_

  def a(*args)
    if args.size > 0
      @a = args[0]
      self
    else
      @a
    end
  end

Modifies attr_writer to return an array of the names of the methods it has defined.

Returns the root name of the module/class.

  require 'facet/module/basename'

  module Example
    class Demo
    end
  end

  Demo.name       #=> Example::Demo
  Demo.basename   #=> Demo

Note: the following documentation uses "class" because it’s more common, but it applies to modules as well.

Given the name of a class, returns the class itself (i.e. instance of Class). The dereferencing starts at Object. That is,

  Class.by_name("String")

is equivalent to

  Object.const_get("String")

The parameter name is expected to be a Symbol or String, or at least to respond to to_str.

An ArgumentError is raised if name does not correspond to an existing class. If name is not even a valid class name, the error you’ll get is not defined.

Examples:

  require 'facet/module/by_name'

  Class.by_name("String")             # -> String
  Class.by_name("::String")           # -> String
  Class.by_name("Process::Sys")       # -> Process::Sys
  Class.by_name("GorillaZ")           # -> (ArgumentError)

  Class.by_name("Enumerable")         # -> Enumerable
  Module.by_name("Enumerable")        # -> Enumerable

Automatically create an initializer assigning the given arguments.

  require 'facet/class/initializer'

  class MyClass
    initializer(:a, "b", :c)
  end

_is equivalent to_

  class MyClass
    def initialize(a, b, c)
      @a,@b,@c = a,b,c
    end
  end

Downside: Initializers defined like this can’t take blocks. This can be fixed when Ruby 1.9 is out.

The initializer will not raise an Exception when the user does not supply a value for each instance variable. In that case it will just set the instance variable to nil. You can assign default values or raise an Exception in the block.

Note: This may be better off as a paramerterized module.

Directive for making your functions faster by trading space for time. When you "memoize" a method/function its results are cached so that later calls with the same arguments returns results in the cache instead of recalculating them.

  require 'facet/module/memoize'

  class T
    def initialize(a)
      @a = a
    end
    def a
      "#{@a ^ 3 + 4}"
    end
    memoize :a
  end

  t = T.new
  t.a.__id__  #=>
  t.a.__id__  #=>

Provides an improved method lookup routnine. It returns a list of methods according to symbol(s) given.

Usable symbols include:

  • :inherited or :ancestors
  • :local or :no_ancestors
  • :public
  • :private
  • :protected
  • :all

If no symbol is given then :public is assumed. Unrecognized symbols raise an error.

  require 'facet/module/methods'

  module Demo
    def test
      puts("Hello World!")
    end
  end

  Demo.methods(:local)    #=> ['test']

Redirect methods to other methods. This simply defines methods by the name of a hash key which calls the method with the name of the hash’s value.

  require 'facet/module/redirect'

  class Example
    redirect :hi => :hello, :hey => :hello
    def hello(name)
      puts "Hello, #{name}."
    end
  end

  e = Example.new
  e.hello("Bob")    #=> "Hello, Bob."
  e.hi("Bob")       #=> "Hello, Bob."
  e.hey("Bob")      #=> "Hello, Bob."

The above class definition is equivalent to:

  class Example
    def hi(*args)
      hello(*args)
    end
    def hey(*args)
      hello(*args)
    end
    def hello
      puts "Hello"
    end
  end

Private Instance methods

Alias a module function so that the alias is also a module function. The typical alias_method does not do this.

  require 'facet/moodule/alias_module_function'

  module Demo
    module_function
    def hello
      "Hello"
    end
  end

  Demo.hello    #=> Hello

  module Demo
    alias_module_function( :hi , :hello )
  end

  Demo.hi       #=> Hello
redef( sym, aliased=true, &blk )

Alias for redefine_method

Creates a new method for a pre-existing method. If aliased is set to true (the default) then the old_method is called subsequent to calling the new definition IF the new call returns nil, otherwise the return value of the new call is returned.

This can not be used to wrap methods that take a block.

  require 'facet/module/redefine_method'

  redefine_method( sym, aliased  ) { |*args| ... }
wrap( sym, &blk )

Alias for wrap_method

Creates a new method wrapping the previous of the same name, passing it into the block definition of the new method.

This can not be used to wrap methods that take a block.

  require 'facet/module/wrap_method'

  wrap_method( sym ) { |old_meth, *args|
    old_meth.call
    ...
  }

[Validate]