rubyactivemerchantshopify-activemerchant

How to override class variable in Ruby


I'm trying to override the 'requires_name' method or its variable on line 315 of https://github.com/activemerchant/active_merchant/blob/master/lib/active_merchant/billing/credit_card.rb so that the check on line 328 is not performed. In Australia, the card name is not normally collected during payment so I'm trying to pass in nils. I have not gotten anything to work.

I've tried class_variable_set like:

ActiveMerchant::Billing::CreditCard.class_variable_set(:@@requires_name, Proc.new do
    false
end)

I've tried requires_name and require_name here. No difference.

I've tried:

ActiveMerchant::Billing::CreditCard.requires_name = Proc.new do
    false
end)

I've tried various other things. How do I override it?


Solution

  • You could override the method, but it's easier to set the value the method is returning:

    ActiveMerchant::Billing::CreditCard.require_name = false
    

    Internally, require_name is stored as a class instance variable:

    ActiveMerchant::Billing::CreditCard.instance_variable_get(:@require_name)
    #=> false