Skip to content

Instantly share code, notes, and snippets.

@lancecarlson
Created March 31, 2012 21:41
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 lancecarlson/2268826 to your computer and use it in GitHub Desktop.
Save lancecarlson/2268826 to your computer and use it in GitHub Desktop.
class Shop
class Warehouse
def self.find(id)
Product.find_by_id(id)
end
end
class Customer
attr_reader :user
def self.find(id)
User.find_by_id(id)
end
def initialize(user)
@user = user
end
def pay(product)
# perform the payment
end
end
class Transaction
attr_reader :customer, :product, :order, :status
def initialize(customer, product)
@customer = customer
@product = product
end
def commit
@status = customer.pay(product)
if success?
commit!
end
end
def commit!
create_order
if order.persisted?
mark_product_as_sold
end
end
def success?
status === true
end
private
def create_order
@order = Order.create(:user => customer, :product => product)
end
def mark_product_as_sold
@product.update_attribute(:sold, true)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment