Sha256: 19f32cca1f974f19e80bfa81fed6989ca7331f1722699242f9cddf2cffe75d8a

Contents?: true

Size: 1.1 KB

Versions: 7

Compression:

Stored size: 1.1 KB

Contents

class Class

  # Prepend an "aspect module" to a class.
  #
  #   class Firetruck
  #     def put_out_fire(option)
  #       "Put out #{option}"
  #     end
  #   end
  #
  #   module FastFiretruck
  #     def put_out_fire(option)
  #       super("very #{option}!")
  #     end
  #   end
  #
  #   Firetruck.prepend(FastFiretruck)
  #
  #   ft = Firetruck.new
  #   ft.put_out_fire('fast') #=> "Put out very fast!"
  #
  # Implementation of this method has some limitations,
  # in that it works by overriding #new and #allocate.
  #
  # CREDIT Trans
  #
  # TODO: Perhaps rename this to preallocate, b/c of
  #       the way it works. It is not really a clean
  #       prepend, like that of Module#prepend.

  def prepend( aspect )
    _new      = method(:new)
    _allocate = method(:allocate)
    (class << self; self; end).class_eval do
      define_method(:new) do |*args|
        o = _new.call(*args)
        o.extend aspect
        o
      end
      define_method(:allocate) do |*args|
        o = _allocate.call(*args)
        o.extend aspect
        o
      end
    end
  end

  #alias_method :preallocate, :prepend
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/class/prepend.rb
facets-2.4.1 lib/facets/class/prepend.rb
facets-2.4.2 lib/core/facets/class/prepend.rb
facets-2.4.3 lib/core/facets/class/prepend.rb
facets-2.4.4 lib/core/facets/class/prepend.rb
facets-2.4.5 lib/core/facets/class/prepend.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/class/prepend.rb