Skip to content

Instantly share code, notes, and snippets.

@nicholasjhenry
Created October 24, 2011 17:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nicholasjhenry/ac1405dd6ffba10089b7 to your computer and use it in GitHub Desktop.
Save nicholasjhenry/ac1405dd6ffba10089b7 to your computer and use it in GitHub Desktop.
Use Case Driven Example for Discussion on Clean Coders
## 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