Last active
August 29, 2015 14:11
-
-
Save nessamurmur/00523528180a056a2a70 to your computer and use it in GitHub Desktop.
DCI w/ Variants
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DigitalDelivery | |
attr_reader :order | |
def self.call(order) | |
digtial_order = order.extend(DigitalOrder) | |
deliverer = new(digital_order) | |
# do stuff to get it delivered | |
deliverer | |
end | |
def initialize(order) | |
@order = order | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DigitalOrder | |
include OrderRecord | |
# behavior for a digital order? | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order | |
def self.variant_from(opts={}) | |
[opts[:type], new(opts)] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module OrderRecord | |
include CoolORM | |
# define schema via a cool dsl and stuff... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nifty-variants' | |
class OrdersController < ApplicationController | |
responds_to :json, :html | |
def create | |
context = cases Order.variant_from(order_params), | |
digital: ->(o) { DigitalDelivery.call(o) }, | |
in_store: ->(o) { InStoreDelivery.call(o) }, | |
home_delivery: ->(o) { HomeDelivery.call(o) } | |
respond_with(context.order) | |
end | |
private | |
def order_params | |
params.permit(:type, :address, :email, :store_id, :item_id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment