Project

General

Profile

Feature #8382 ยป ostruct_yaml.patch

pietro (Pietro Monteiro), 05/08/2013 01:07 PM

View differences:

ChangeLog
Wed May 8 10:54:09 2013 Pietro Monteiro <pietro@riseup.net>
* lib/ostruct.rb (encode_with, init_with): Format YAML dump and
create getters and setters after load.
Wed May 8 06:42:56 2013 NARUSE, Yui <naruse@ruby-lang.org>
* ext/socket/socket.c (socket_s_ip_address_list): fix wrongly filled
lib/ostruct.rb
end
#
# Provides marshalling support for use by the YAML library.
#
def encode_with(coder)
@table.each_pair do |key, value|
coder[key.to_s] = value
end
end
#
# Provides marshalling support for use by the YAML library.
#
def init_with(coder)
@table = {}
coder.map.each_pair do |key, value|
key = key.to_sym
@table[key] = value
new_ostruct_member(key)
end
end
#
# Used internally to check if the OpenStruct is able to be
# modified before granting access to the internal Hash table to be modified.
#
test/ostruct/test_ostruct.rb
require 'test/unit'
require 'ostruct'
require 'yaml'
class TC_OpenStruct < Test::Unit::TestCase
def test_initialize
......
assert_equal true, os1.eql?(os1.dup)
assert_equal os1.hash, os1.dup.hash
end
def test_yaml
h = {name: "John Smith", age: 70, pension: 300.0}
yaml = "--- !ruby/object:OpenStruct\nname: John Smith\nage: 70\npension: 300.0\n"
os1 = OpenStruct.new(h)
os2 = YAML.load(os1.to_yaml)
assert_equal yaml, os1.to_yaml
assert_equal os1, os2
assert_equal true, os1.eql?(os2)
assert_equal os1.methods, os2.methods
end
end
    (1-1/1)