mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-12-06 00:00:29 -05:00
Merge pull request #592 from stripe/brandur-reimplement-custom-marshal
Implement custom Marshal encoder/decoder for `StripeObject`
This commit is contained in:
commit
f543771003
@ -144,6 +144,28 @@ module Stripe
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Implements custom encoding for Ruby's Marshal. The data produced by this
|
||||||
|
# method should be comprehendable by #marshal_load.
|
||||||
|
#
|
||||||
|
# This allows us to remove certain features that cannot or should not be
|
||||||
|
# serialized.
|
||||||
|
def marshal_dump
|
||||||
|
# The StripeClient instance in @opts is not serializable and is not
|
||||||
|
# really a property of the StripeObject, so we exclude it when
|
||||||
|
# dumping
|
||||||
|
opts = @opts.clone
|
||||||
|
opts.delete(:client)
|
||||||
|
[@values, opts]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Implements custom decoding for Ruby's Marshal. Consumes data that's
|
||||||
|
# produced by #marshal_dump.
|
||||||
|
def marshal_load(data)
|
||||||
|
values, opts = data
|
||||||
|
initialize(values[:id])
|
||||||
|
initialize_from(values, opts)
|
||||||
|
end
|
||||||
|
|
||||||
def serialize_params(options = {})
|
def serialize_params(options = {})
|
||||||
update_hash = {}
|
update_hash = {}
|
||||||
|
|
||||||
|
|||||||
@ -410,5 +410,18 @@ module Stripe
|
|||||||
end
|
end
|
||||||
assert_match(/\(object\).foo = nil/, e.message)
|
assert_match(/\(object\).foo = nil/, e.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "marshal and unmarshal using custom encoder and decoder" do
|
||||||
|
obj = Stripe::StripeObject.construct_from(
|
||||||
|
{ id: 1, name: "Stripe" },
|
||||||
|
api_key: "apikey",
|
||||||
|
client: StripeClient.active_client
|
||||||
|
)
|
||||||
|
m = Marshal.load(Marshal.dump(obj))
|
||||||
|
assert_equal 1, m.id
|
||||||
|
assert_equal "Stripe", m.name
|
||||||
|
expected_hash = { api_key: "apikey" }
|
||||||
|
assert_equal expected_hash, m.instance_variable_get("@opts")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user