-
-
Save nicholasjhenry/ac1405dd6ffba10089b7 to your computer and use it in GitHub Desktop.
Use Case Driven Example for Discussion on Clean Coders
This file contains 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 Packages the Application | |
# lib/pay_roll.rb | |
module PayRoll | |
class << self | |
attr_accessor :database | |
def config | |
yield self | |
end | |
end | |
end | |
# lib/pay_roll/pay_day_transaction.rb | |
class PayRoll::PayDayTransaction | |
def initialize(date=Date.today) | |
@date = date | |
end | |
def execute | |
employees = PayRoll.database.find_employees | |
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/employee.rb | |
module PayRoll::Employee | |
def pay_date?(date) | |
# ... | |
end | |
def send_pay(pay_check) | |
# ... | |
end | |
end | |
# lib/pay_roll/pay_check.rb | |
class PayRoll::PayCheck | |
# ... | |
end | |
## Rails as a delivery service for the PayRoll Application | |
# config/initializers/pay_roll.rb | |
PayRoll.config do |config| | |
config.database = PayRollDatabase.new | |
end | |
# models/pay_roll_database.rb | |
class PayRollDatabase | |
def find_employees | |
Employee.active | |
end | |
end | |
# models/employee.rb | |
class Employee < ActiveRecord::Base | |
include PayRoll::Employee | |
scope :active, where(:active => true) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment