From 44bad70987aedc12b30f895cbb1146d3da7bce12 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Fri, 14 Apr 2017 09:31:58 -0500 Subject: [PATCH] Include predicate for lazily added boolean accessors Previously, the value of whatever accessor was missing was left out of the call to build it. This had the effect of skipping over the lazily-built predicate method when the missing accessor is for a boolean. Of course, I realize this all is a bit contrived as most of the time folks aren't assigning these values manually to stripe objects. However, in my testing it caught me by surprised that the behavior is asymmetric depending on how and when values are assigned. As such I believe this is a less surprising implementation. --- lib/stripe/stripe_object.rb | 3 ++- test/stripe/stripe_object_test.rb | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/stripe/stripe_object.rb b/lib/stripe/stripe_object.rb index 4ea943e5..2b0509b2 100644 --- a/lib/stripe/stripe_object.rb +++ b/lib/stripe/stripe_object.rb @@ -262,9 +262,10 @@ module Stripe # TODO: only allow setting in updateable classes. if name.to_s.end_with?('=') attr = name.to_s[0...-1].to_sym + val = args.first # the second argument is only required when adding boolean accessors - add_accessors([attr], {}) + add_accessors([attr], { attr => val }) begin mth = method(name) diff --git a/test/stripe/stripe_object_test.rb b/test/stripe/stripe_object_test.rb index a5246a0c..b9598124 100644 --- a/test/stripe/stripe_object_test.rb +++ b/test/stripe/stripe_object_test.rb @@ -80,6 +80,13 @@ module Stripe refute obj.respond_to?(:not_bool?) end + should "assign question mark accessors for booleans added after initialization" do + obj = Stripe::StripeObject.new + obj.bool = true + assert obj.respond_to?(:bool?) + assert obj.bool? + end + should "mass assign values with #update_attributes" do obj = Stripe::StripeObject.construct_from({ :id => 1, :name => 'Stripe' }) obj.update_attributes(:name => 'STRIPE')