Sha256: 89a103394de1e73001eca4df2752b3d63a2ba4dd7a5e0ab49a36abbd1c53463d

Contents?: true

Size: 704 Bytes

Versions: 4

Compression:

Stored size: 704 Bytes

Contents

require 'ostruct'
require 'facet/hash/to_h'
require 'facet/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
  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

  end

=end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-1.0.0 lib/facet/ostruct/__update__.rb
facets-1.0.3 packages/core/lib/facet/ostruct/__update__.rb
facets-1.1.0 lib/facet/ostruct/__update__.rb
facets-1.2.0 lib/facets/core/ostruct/__update__.rb