mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-12-03 00:01:19 -05:00
Merge pull request #625 from stripe/ob-skip-to-hash-for-nil
Skip calling to_hash for nil
This commit is contained in:
commit
1abb8a574b
@ -116,7 +116,7 @@ module Stripe
|
|||||||
|
|
||||||
def to_hash
|
def to_hash
|
||||||
maybe_to_hash = lambda do |value|
|
maybe_to_hash = lambda do |value|
|
||||||
value.respond_to?(:to_hash) ? value.to_hash : value
|
value && value.respond_to?(:to_hash) ? value.to_hash : value
|
||||||
end
|
end
|
||||||
|
|
||||||
@values.each_with_object({}) do |(key, value), acc|
|
@values.each_with_object({}) do |(key, value), acc|
|
||||||
|
|||||||
@ -115,23 +115,40 @@ module Stripe
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "recursively call to_hash on its values" do
|
context "#to_hash" do
|
||||||
# deep nested hash (when contained in an array) or StripeObject
|
should "skip calling to_hash on nil" do
|
||||||
nested_hash = { id: 7, foo: "bar" }
|
module NilWithToHash
|
||||||
nested = Stripe::StripeObject.construct_from(nested_hash)
|
def to_hash
|
||||||
|
raise "Can't call to_hash on nil"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
# include is private in Ruby 2.0
|
||||||
|
NilClass.send(:include, NilWithToHash)
|
||||||
|
|
||||||
obj = Stripe::StripeObject.construct_from(id: 1,
|
hash_with_nil = { id: 3, foo: nil }
|
||||||
# simple hash that contains a StripeObject to help us test deep
|
obj = StripeObject.construct_from(hash_with_nil)
|
||||||
# recursion
|
expected_hash = { id: 3, foo: nil }
|
||||||
nested: { object: "list", data: [nested] },
|
assert_equal expected_hash, obj.to_hash
|
||||||
list: [nested])
|
end
|
||||||
|
|
||||||
expected_hash = {
|
should "recursively call to_hash on its values" do
|
||||||
id: 1,
|
# deep nested hash (when contained in an array) or StripeObject
|
||||||
nested: { object: "list", data: [nested_hash] },
|
nested_hash = { id: 7, foo: "bar" }
|
||||||
list: [nested_hash],
|
nested = Stripe::StripeObject.construct_from(nested_hash)
|
||||||
}
|
|
||||||
assert_equal expected_hash, obj.to_hash
|
obj = Stripe::StripeObject.construct_from(id: 1,
|
||||||
|
# simple hash that contains a StripeObject to help us test deep
|
||||||
|
# recursion
|
||||||
|
nested: { object: "list", data: [nested] },
|
||||||
|
list: [nested])
|
||||||
|
|
||||||
|
expected_hash = {
|
||||||
|
id: 1,
|
||||||
|
nested: { object: "list", data: [nested_hash] },
|
||||||
|
list: [nested_hash],
|
||||||
|
}
|
||||||
|
assert_equal expected_hash, obj.to_hash
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
should "assign question mark accessors for booleans" do
|
should "assign question mark accessors for booleans" do
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user