require 'facets/core/kernel/set_with'

module Kernel

  # Set setter methods using a another object.
  #
  #   class X
  #     attr_accessor :a, :b
  #     def initialize( a, b )
  #        @a,@b = a,b
  #     end
  #   end
  #
  #   obj1 = X.new( 1, 2 )
  #   obj2 = X.new
  #
  #   obj2.set_from(obj1)
  #
  #   obj2.a  #=> 1
  #   obj2.b  #=> 2
  #
  def set_from(obj, *fields)
    unless fields.empty?
      fields.each do |k|
        send( "#{k}=", obj.send("#{k}") )  #if self.respond_to?("#{k}=") && obj.respond_to?("#{k}")
      end
    else
      setters = methods.collect { |m| m =~ /=$/ }
      setters.each do |setter|
        getter = setter.chomp('=')
        if obj.respond_to?(getter)
          send( setter, obj.send(getter) )
          fields < getter
        end
      end
    end
    fields
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCKernel < Test::Unit::TestCase

    Customer = Struct.new( "Customer", :name, :address, :zip )

    def test_set_from
      bob = Customer.new("Bob Sawyer", "123 Maple, Anytown NC", 12345)
      joe = Customer.new("Joe Pitare")
      joe.set_from(bob, :address, :zip)
      assert_equal("Joe Pitare", joe.name)
      assert_equal("123 Maple, Anytown NC", joe.address)
      assert_equal(12345, joe.zip)  
    end

  end

=end
