-
-
Save rinaldifonseca/ecbd0bf9520f165db120cbbe9294ca56 to your computer and use it in GitHub Desktop.
PayRoll application, embedded in Rails, borrowing from Use Case Driven Architecture and DCI
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
## PayRoll gem | |
# lib/pay_roll.rb | |
module PayRoll | |
class << self | |
attr_accessor :employee_directory | |
def config | |
yield self | |
end | |
end | |
end | |
end | |
# lib/pay_roll/services/pay_day_service.rb | |
module PayRoll | |
class PayDayService | |
def initialize(date=Date.today, options={}) | |
@date = date | |
options[:employees] ||= PayRoll.employee_directory.active | |
@employees = options[:employees].each { |e| e.extend(Payable) } | |
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 | |
end | |
# lib/pay_roll/roles/payable.rb | |
module Payable | |
def pay_date?(date) | |
# ... | |
end | |
def send_pay(pay_check) | |
# ... | |
end | |
end | |
## Rails application | |
# app/controllers/pay_day_controller.rb | |
class PayDayController < ApplicationController | |
def create | |
PayRoll::PayDayService.new.execute | |
end | |
end | |
# config/initializers/pay_roll.rb | |
PayRoll.config do |config| | |
employee_directory = EmployeeDirectory.new # alternatively just use Employee model | |
end | |
# models/employee_directory.rb | |
class EmployeeDirectory | |
def active | |
Employee.active | |
end | |
end | |
# models/employee.rb | |
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