Skip to content

Instantly share code, notes, and snippets.

@davetron5000
Last active December 30, 2022 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetron5000/f7e86fd909b26186b420d9b79cae0e5d to your computer and use it in GitHub Desktop.
Save davetron5000/f7e86fd909b26186b420d9b79cae0e5d to your computer and use it in GitHub Desktop.
class ChargeCustomer
def charge!(customer:, amount:, note:)
# do something
end
end
class ChargeParams
def initialize(order)
@order = order
end
def to_hash # this is the magic of double-splat
{
customer: @order.customer,
amount: @order.total,
note: "Purchased #{@order.product.description}",
}
end
end
order = Order.find(order_id)
charge_params = ChargeParams.new(order)
charge_customer = ChargeCustomer.new
charge_customer.charge!(**charge_params)
# ^^
# ||
# calls to_hash, then
# coerces to kwargs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment