Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Module#delegate_missing_to #23930

Merged
merged 1 commit into from May 24, 2016

Commits on Feb 27, 2016

  1. 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 rails#23824.
    gsamokovarov committed Feb 27, 2016
    Copy the full SHA
    335fcc2 View commit details
    Browse the repository at this point in the history