Sha256: 6c52ad76acaf965d20d157b864fc712fca75d3a84dc1665614898ee835c62bf8

Contents?: true

Size: 1.51 KB

Versions: 10

Compression:

Stored size: 1.51 KB

Contents

# = RWDelegator
#
# WARNING Highly expiremental code!
#
# Read from one object and write to another.
#
# == Authors
#
# * Thomas Sawyer
#
# == Copying
#
# Copyright (c) 2005 Thomas Sawyer
#
# Ruby License
#
# This module is free software. You may use, modify, and/or redistribute this
# software under the same terms as Ruby.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.

# = RWDelegator
#
# WARNING Highly expiremental code!
#
# Read from one object and write to another.
#
class RWDelegator

  def initialize( write, &read )
    @read = read
    @write = write

    # ensure other classes can deduce equality.
    read_class = @read.call.object_class
    unless read_class.method_defined?(:eq_with_rwdelegator?)
      read_class.class_eval %{
        def eq_with_rwdelegator?( other )
          if RWDelegator === other
            other == self
          else
            eq_without_rwdelegator?(other)
          end
        end
        alias_method :eq_without_rwdelegator?, :==
        alias_method :==, :eq_with_rwdelegator?
      }
    end
  end

  def inspect
    "#<#{object_class} #{@read.call.inspect}>"
  end

  def ==( other )
    @read.call == other
  end

  def method_missing( meth, *args, &blk )
    read = @read.call
    ditto = read.dup
    result = ditto.send( meth, *args, &blk )
    if ditto != read
      result = @write.send( meth, *args, &blk )
    end
    result
  end

end



Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/rwdelegator.rb
facets-2.4.1 lib/facets/rwdelegator.rb
facets-2.4.3 lib/more/facets/rwdelegator.rb
facets-2.4.4 lib/more/facets/rwdelegator.rb
facets-2.4.2 lib/more/facets/rwdelegator.rb
facets-2.5.1 lib/more/facets/rwdelegator.rb
facets-2.4.5 lib/more/facets/rwdelegator.rb
facets-2.5.0 lib/more/facets/rwdelegator.rb
facets-2.5.2 lib/more/facets/rwdelegator.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/more/facets/rwdelegator.rb