-
Notifications
You must be signed in to change notification settings - Fork 2
cavalle edited this page Dec 23, 2012
·
3 revisions
http://mikepackdev.com/blog_posts/24-the-right-way-to-code-dci-in-ruby
# app/roles/customer.rb
module Customer
def add_to_cart(book)
self.cart << book
end
end# app/contexts/add_to_cart_context.rb
class AddToCartContext
attr_reader :user, :book
def self.call(user, book)
AddToCartContext.new(user, book).call
end
def initialize(user, book)
@user, @book = user, book
@user.extend Customer
end
def call
@user.add_to_cart(@book)
end
endclass Book < ActiveRecord::Base
validates :title, :presence => true
end# app/controllers/book_controller.rb
class BookController < ApplicationController
def add_to_cart
AddToCartContext.call(current_user, Book.find(params[:id]))
end
end# app/models/user.rb
class User < ActiveRecord::Base
# A dumb data object
end# app/roles/customer.rb
module Customer
def purchase(book)
# Update the system to record purchase
end
end# app/use_cases/customer_purchases_book.rb
class CustomerPurchasesBook
def initialize(user, book)
@customer, @book = user, book
@customer.extend(Customer)
end
def call
@customer.purchase(@book)
end
end