Skip to content

Instantly share code, notes, and snippets.

@victorbstan
Forked from nicholasjhenry/your_application.rb
Created October 28, 2011 12:31
Show Gist options
  • Save victorbstan/1322159 to your computer and use it in GitHub Desktop.
Save victorbstan/1322159 to your computer and use it in GitHub Desktop.
PayRoll application, embedded in Rails, borrowing from Use Case Driven Architecture and DCI
## PayRoll Application Gem
# lib/pay_roll/pay_day_service.rb
# Consider this Use Case as the Context in DCI
#
class PayRoll::PayDayService
def initialize(date=Date.today)
@date = date
# Employee could be any data source from the host,
# in this example Rails/Active Record
#
@employees ||= Employee.active
@employees.each { |e| e.extend(PayCheckRecipient) }
end
def execute
@employees.each do |e|
if e.pay_date?(@date)
pc = PayCheck.new(e.calculate_pay(@date))
e.send_pay(pc)
end
end
end
end
# lib/pay_roll/pay_check_recipient.rb
# Consider this the Role in DCI
#
module PayRoll::PayCheckRecipient
def pay_date?(date)
# ...
end
def send_pay(pay_check)
# ...
end
end
# lib/pay_roll/pay_check.rb
#
class PayRoll::PayCheck
# ...
end
## Rails application
# app/controllers/pay_day_controller.rb
# Yes, this would make more sense to be run in a scheduled job, but wanted to show
# an example of services used in a Rails controller
#
class PayDayController < ApplicationController
def create
PayRoll::PayDayService.new.execute
redirect_to :back, :notice => "Pay day has been successfully completed"
end
end
# models/employee.rb
# The data in DCI
#
class Employee < ActiveRecord::Base
scope :active, where(:active => true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment