aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorT.J. Schuck <tj@tjschuck.com>2017-05-04 15:43:19 -0400
committerT.J. Schuck <tj@tjschuck.com>2017-05-04 16:29:00 -0400
commitaa913280584ba63bfc461d0009abd6c32525da9c (patch)
treef814bde4b4fe689b8c84f5763168edd67fb561ef /activesupport
parent3d890b66c1bfbdcabb7ef66e0774e0f01e2ed5d6 (diff)
downloadrails-aa913280584ba63bfc461d0009abd6c32525da9c.tar.gz
rails-aa913280584ba63bfc461d0009abd6c32525da9c.tar.bz2
rails-aa913280584ba63bfc461d0009abd6c32525da9c.zip
Assorted delegate_missing_to doc fixes
* Fix rdoc code formatting — `tt`, not backticks * Fix/simplify sentence grammar — should at least just be “and the like”, not “likes”, but this is just general tightening up. * Add note that delegated methods must be public. Tested here: https://github.com/rails/rails/blob/7ff5ccae94ce2aff76b5f8a31a28e305a047d642/activesupport/test/core_ext/module_test.rb#L359-L365 * Simplify example code for delegate_missing_to. The example had complexity that wasn’t necessary for demonstrating `delegate_missing_to`. This gets rid of a bunch of cruft so the example is more obvious about what’s going on regarding the feature itself. [ci skip]
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/module/delegation.rb39
1 files changed, 17 insertions, 22 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb
index 85ab739095..13f3894e6c 100644
--- a/activesupport/lib/active_support/core_ext/module/delegation.rb
+++ b/activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -219,48 +219,43 @@ class Module
# When building decorators, a common pattern may emerge:
#
# class Partition
- # def initialize(first_event)
- # @events = [ first_event ]
+ # def initialize(event)
+ # @event = event
# end
#
- # def people
- # if @events.first.detail.people.any?
- # @events.collect { |e| Array(e.detail.people) }.flatten.uniq
- # else
- # @events.collect(&:creator).uniq
- # end
+ # def person
+ # @event.detail.person || @event.creator
# end
#
# private
# def respond_to_missing?(name, include_private = false)
- # @events.respond_to?(name, include_private)
+ # @event.respond_to?(name, include_private)
# end
#
# def method_missing(method, *args, &block)
- # @events.send(method, *args, &block)
+ # @event.send(method, *args, &block)
# end
# end
#
- # With `Module#delegate_missing_to`, the above is condensed to:
+ # With <tt>Module#delegate_missing_to</tt>, the above is condensed to:
#
# class Partition
- # delegate_missing_to :@events
+ # delegate_missing_to :@event
#
- # def initialize(first_event)
- # @events = [ first_event ]
+ # def initialize(event)
+ # @event = event
# end
#
- # def people
- # if @events.first.detail.people.any?
- # @events.collect { |e| Array(e.detail.people) }.flatten.uniq
- # else
- # @events.collect(&:creator).uniq
- # end
+ # def person
+ # @event.detail.person || @event.creator
# end
# end
#
- # The target can be anything callable within the object. E.g. instance
- # variables, methods, constants and the likes.
+ # The target can be anything callable within the object, e.g. instance
+ # variables, methods, constants, etc.
+ #
+ # The delegated method must be public on the target, otherwise it will
+ # raise +NoMethodError+.
def delegate_missing_to(target)
target = target.to_s
target = "self.#{target}" if DELEGATION_RESERVED_METHOD_NAMES.include?(target)