diff options
author | Genadi Samokovarov <gsamokovarov@gmail.com> | 2016-02-27 19:23:42 +0200 |
---|---|---|
committer | Genadi Samokovarov <gsamokovarov@gmail.com> | 2016-02-27 19:26:27 +0200 |
commit | 335fcc214a4b7ea5e10b3610cce6faea4262e4f6 (patch) | |
tree | 56f70b4e0518aa5a33c30a05ad5021dc2e95c172 /activesupport/lib/active_support/testing/isolation.rb | |
parent | ecf6dc3dfbc0cdf1b0a86f48e030debd0f917cb6 (diff) | |
download | rails-335fcc214a4b7ea5e10b3610cce6faea4262e4f6.tar.gz rails-335fcc214a4b7ea5e10b3610cce6faea4262e4f6.tar.bz2 rails-335fcc214a4b7ea5e10b3610cce6faea4262e4f6.zip |
Introduce Module#delegate_missing_to
When building decorators, a common pattern may emerge:
class Partition
def initialize(first_event)
@events = [ first_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
end
private
def respond_to_missing?(name, include_private = false)
@events.respond_to?(name, include_private)
end
def method_missing(method, *args, &block)
@events.send(method, *args, &block)
end
end
With `Module#delegate_missing_to`, the above is condensed to:
class Partition
delegate_missing_to :@events
def initialize(first_event)
@events = [ first_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
end
end
David suggested it in #23824.
Diffstat (limited to 'activesupport/lib/active_support/testing/isolation.rb')
0 files changed, 0 insertions, 0 deletions