lib/balanced/resources/card.rb in balanced-0.7.2 vs lib/balanced/resources/card.rb in balanced-0.7.4
- old
+ new
@@ -34,10 +34,12 @@
appears_on_statement_as = args[1] || options.fetch(:appears_on_statement_as) { nil }
hold_uri = args[2] || options.fetch(:hold_uri) { nil }
meta = args[3] || options.fetch(:meta) { nil }
description = args[3] || options.fetch(:description) { nil }
+ ensure_associated_to_account!
+
self.account.debit(
:amount => amount,
:appears_on_statement_as => appears_on_statement_as,
:hold_uri => hold_uri,
:meta => meta,
@@ -53,10 +55,12 @@
warn_on_positional args
options = args.last.is_a?(Hash) ? args.pop : {}
amount = args[0] || options.fetch(:amount) { nil }
meta = args[1] || options.fetch(:meta) { nil }
+ ensure_associated_to_account!
+
self.account.hold(
:amount => amount,
:meta => meta,
:source_uri => self.uri
)
@@ -65,8 +69,16 @@
def invalidate
self.is_valid = false
save
end
+ private
+ # Ensure that one of account, account_uri, customer or customer_uri are set.
+ # Otherwise raise an exception.
+ def ensure_associated_to_account!
+ if attributes.values_at('account', 'account_uri', 'customer', 'customer_uri').compact.empty?
+ raise UnassociatedCardError.new(self)
+ end
+ end
end
end