summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2020-09-26 01:27:23 -0400
committerMarc-André Lafortune <github@marc-andre.ca>2020-09-30 18:11:24 -0400
commitb36a45c05cafc227ade3b59349482953321d6a89 (patch)
treed166ac4aa87cab243740e599dc186c23e2c4ce1a /lib
parent0e93118c44fc4128bcacfe1dc6702c84a84b862b (diff)
[ruby/ostruct] Improved YAML serialization.
Patch adapted from Pietro Monteiro [Fixes bug#8382]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3593
Diffstat (limited to 'lib')
-rw-r--r--lib/ostruct.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index d2e93b1463..4ecffb094e 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -398,6 +398,33 @@ class OpenStruct
@table.hash
end
+ #
+ # Provides marshalling support for use by the YAML library.
+ #
+ def encode_with(coder) # :nodoc:
+ @table.each_pair do |key, value|
+ coder[key.to_s] = value
+ end
+ if @table.size == 1 && @table.key?(:table) # support for legacy format
+ # in the very unlikely case of a single entry called 'table'
+ coder['legacy_support!'] = true # add a bogus second entry
+ end
+ end
+
+ #
+ # Provides marshalling support for use by the YAML library.
+ #
+ def init_with(coder) # :nodoc:
+ h = coder.map
+ if h.size == 1 # support for legacy format
+ key, val = h.first
+ if key == 'table'
+ h = val
+ end
+ end
+ update_to_values!(h)
+ end
+
# Make all public methods (builtin or our own) accessible with `!`:
instance_methods.each do |method|
new_name = "#{method}!"