Sha256: 47d3cd7f2234f4c2f0b391637eeb2e8bfe2b22db2f033ecafc05cc5854ba68a1

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

#   Copyright (c) 2008 Ben Woodcroft <donttrustben somewhere near gmail.com>
#
#   This program is free software.
#   You can distribute/modify this program under the terms of
#   the GNU General Public License version 3.

# The point of this is that the 
# Bookshelf.all.reach.books.each do {|book| puts book.name}
# instead of
# Bookshelf.all.each do |bookshelf|
# bookshelf.books.each do |book|
# puts book.name
# end
# end

# Bookshelf.all.reach is a ReachableArray
# the books is an array (bookshelves) of arrays (books)
class Array
  def reach
    ReachingArray.new(self)
  end
end

class ReachingArray
  # The array that this reaching array is extending. This might
  # be a real Array, or a ReachingArray
  attr_accessor :retract
  
  def initialize(retract)
    @retract = retract
  end
  
  # The method could be missing from the array, the members, or both.
  # Try to pass the method to each, in that order, accepting the first taker
  def method_missing(method, *args, &block)
    if @retract.respond_to?(method)
      # If this is an array method, just use that
      r = @retract.send(method, *args, &block)
      return r
    else
      # If this is an object-specific method, run an each on it.
      # A simple each won't work because that doesn't modify the
      # array elements in place as we want, so have to use collect
      # instead.
      @retract = @retract.collect do |o|
        o.send(method, *args, &block)
      end
      return self
    end
  end
  
  # Equality test - equality of the underlying retract
  # arrays is all that matter
  def ==(another)
    @retract <=> another.retract
  end
  
  def to_s
    @retract.to_s
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wwood-reach-0.1.1 lib/reach.rb