Sha256: 9c95a083c33f61c57fc6bbf33a6098fc50fc828a1a4db47321ea169fd2ed3f8f

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

#--
# Author::    Tyler Rick
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
# License::   Ruby License
# Submit to Facets?:: Yes.
# Developer notes::
# Changes::
#++

$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
gem 'facets'
require 'facets/kernel/singleton_class'
require 'facets/module/alias_method_chain'

class Module

  # Same as <tt>Module#alias_method_chain</tt>, only it works for modules/classes
  #
  #   class X
  #     def self.foo
  #       'foo'
  #     end
  #     malias_method_chain :foo, :feature
  #   end
  #
  # Note: You could always do the same thing with <tt>Module#alias_method_chain</tt> by simply doing this:
  #
  #   class << self
  #     alias_method_chain :foo, :feature
  #   end
  #
  def malias_method_chain(target, feature, *args)
    # Strip out punctuation on predicates or bang methods since
    # e.g. target?_without_feature is not a valid method name.

    singleton_class.instance_eval do
      alias_method_chain target, feature, *args
    end
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test
require 'test/unit'

class TestHowYouWouldDoItWithPlain_alias_method_chain < Test::Unit::TestCase

  class X
    def self.foo
      'foo'
    end
    def self.foo_with_feature
      foo_without_feature + '_with_feature'
    end
    class << self
      alias_method_chain :foo, :feature
    end
  end

  def test_001
    assert_equal 'foo_with_feature', X.foo
  end

end

class Test_malias_method_chain < Test::Unit::TestCase

  class Y
    def self.foo
      'foo'
    end
    def self.foo_with_feature
      foo_without_feature + '_with_feature'
    end
    malias_method_chain :foo, :feature
  end

  def test_001
    assert_equal 'foo_with_feature', Y.foo
  end

end

=end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quality_extensions-1.4.0 lib/quality_extensions/module/malias_method_chain.rb