Skip to content

Instantly share code, notes, and snippets.

@nessamurmur
Last active August 29, 2015 14:11
Show Gist options
  • Save nessamurmur/00523528180a056a2a70 to your computer and use it in GitHub Desktop.
Save nessamurmur/00523528180a056a2a70 to your computer and use it in GitHub Desktop.
DCI w/ Variants
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
module DigitalOrder
include OrderRecord
# behavior for a digital order?
end
class Order
def self.variant_from(opts={})
[opts[:type], new(opts)]
end
end
module OrderRecord
include CoolORM
# define schema via a cool dsl and stuff...
end
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