Froze rails gems
[depot.git] / vendor / rails / activesupport / lib / active_support / core_ext / module / aliasing.rb
1 module ActiveSupport
2 module CoreExtensions
3 module Module
4 # Encapsulates the common pattern of:
5 #
6 # alias_method :foo_without_feature, :foo
7 # alias_method :foo, :foo_with_feature
8 #
9 # With this, you simply do:
10 #
11 # alias_method_chain :foo, :feature
12 #
13 # And both aliases are set up for you.
14 #
15 # Query and bang methods (foo?, foo!) keep the same punctuation:
16 #
17 # alias_method_chain :foo?, :feature
18 #
19 # is equivalent to
20 #
21 # alias_method :foo_without_feature?, :foo?
22 # alias_method :foo?, :foo_with_feature?
23 #
24 # so you can safely chain foo, foo?, and foo! with the same feature.
25 def alias_method_chain(target, feature)
26 # Strip out punctuation on predicates or bang methods since
27 # e.g. target?_without_feature is not a valid method name.
28 aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
29 yield(aliased_target, punctuation) if block_given?
30
31 with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
32
33 alias_method without_method, target
34 alias_method target, with_method
35
36 case
37 when public_method_defined?(without_method)
38 public target
39 when protected_method_defined?(without_method)
40 protected target
41 when private_method_defined?(without_method)
42 private target
43 end
44 end
45
46 # Allows you to make aliases for attributes, which includes
47 # getter, setter, and query methods.
48 #
49 # Example:
50 #
51 # class Content < ActiveRecord::Base
52 # # has a title attribute
53 # end
54 #
55 # class Email < Content
56 # alias_attribute :subject, :title
57 # end
58 #
59 # e = Email.find(1)
60 # e.title # => "Superstars"
61 # e.subject # => "Superstars"
62 # e.subject? # => true
63 # e.subject = "Megastars"
64 # e.title # => "Megastars"
65 def alias_attribute(new_name, old_name)
66 module_eval <<-STR, __FILE__, __LINE__+1
67 def #{new_name}; self.#{old_name}; end
68 def #{new_name}?; self.#{old_name}?; end
69 def #{new_name}=(v); self.#{old_name} = v; end
70 STR
71 end
72 end
73 end
74 end