aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/module/aliasing.rb
blob: 5c5875504f7d788ff02929254839e88499f7b0ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Module
  # Encapsulates the common pattern of:
  #
  #   alias_method :foo_without_feature, :foo
  #   alias_method :foo, :foo_with_feature
  #
  # With this, you simply do:
  #
  #   alias_method_chain :foo, :feature
  #
  # And both aliases are set up for you.
  #
  # A punctuation is moved to the end on predicates or bang methods.
  #
  #   alias_method_chain :foo?, :feature
  # 
  # generates "foo_without_feature?" method for old one,
  # and expects "foo_with_feature?" method for new one.
  def alias_method_chain(target, feature)
    punctuation    = target.to_s.scan(/[?!]/).first
    aliased_target = target.to_s.sub(/[?!]/, '')
    alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target
    alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
  end
end