warn 'use #instance_delegate for future version'

require 'ostruct'
require 'facets/core/hash/to_h'
require 'facets/core/ostruct/to_h'

class OpenStruct

  # Insert/update hash data on the fly.
  #
  #   o = OpenStruct.new
  #   o.ostruct_update { :a => 2 }
  #   o.a  #=> 2
  #
  def __update__( other )
    hash = other.to_h
    for k,v in hash
      @table[k.to_sym] = v
    end
    self
  end

  # Merge hash data creating a new OpenStruct object.
  #
  #   o = OpenStruct.new
  #   o.ostruct_merge { :a => 2 }
  #   o.a  #=> 2
  #
  def __merge__( other )
    o = dup
    o.__update__( other )
    o
  end

end


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

  require 'test/unit'

  class TCOpenStruct < Test::Unit::TestCase

    def test_update
      o = OpenStruct.new( { :a => 1 } )
      h = { :b => 2 }
      assert_nothing_raised { o.__update__( h ) }
      assert_equal( 2, o.b )
    end

    def test_merge_1
      o = OpenStruct.new( { :a => 1 } )
      h = { :b => 2 }
      q = o.__merge__( h )
      assert_equal( 1, q.a )
      assert_equal( 2, q.b )
    end

    def test_merge_2
      o1 = OpenStruct.new( { :a => 1 } )
      o2 = OpenStruct.new( { :b => 2 } )
      q = o1.__merge__( o2 )
      assert_equal( 1, q.a )
      assert_equal( 2, q.b )
    end

  end

=end
