Prev | Current Page 103 | Next

Brad Ediger

"Advanced Rails"

rb
??? alias_method_chain(target, feature) wraps a method in a new function, usually to
add a new feature. This mechanism was discussed in Chapter 1. The method call:
alias_method_chain :target, :feature
is equivalent to:
alias_method :target_without_feature, :target
alias_method :target, :target_with_feature
One consequence is that the target_with_feature method must exist before the alias_
method_chain call. Punctuation (at the end of ?, !, and = methods) is properly moved to
the end of the method names.
??? alias_attribute(new_name, old_name) aliases an ActiveRecord attribute to a new name,
adding the getter, setter, and predicate (for example, person.name?) methods.
Delegation core_ext/module/delegation.rb
The delegate method provides a simple way to delegate one or more methods to an object:
class StringProxy
attr_accessor :target
delegate :to_s, :to => :target
def initialize(target)
@target = target
end
end
proxy = StringProxy.new("Hello World!")
proxy.to_s # => "Hello World!"
proxy.target = "Goodbye World"
proxy.to_s # => "Goodbye World"
More detailed control over delegation can be obtained with Delegator from the standard
library.
Introspection core_ext/module/inclusion.rb, core_ext/module/introspection.rb
??? Module#included_in_classes iterates over all classes (using ObjectSpace), collecting the
classes and modules in which the receiver is included.


Pages:
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115