Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @nicholasjhenry nicholasjhenry revised this gist Dec 22, 2011. 1 changed file with 16 additions and 15 deletions.
    31 changes: 16 additions & 15 deletions your_application.rb
    Original file line number Diff line number Diff line change
    @@ -12,28 +12,26 @@ def config
    end

    # lib/pay_roll/services/pay_day_service.rb
    module PayRoll
    class PayDayService
    def initialize(date=Date.now)
    @date = date

    @employees = PayRoll.employee_directory.active
    @employees.each { |e| e.extend(Payable) }
    end
    class PayRoll::PayDayService
    def initialize(date=Date.now)
    @date = date

    @employees = PayRoll.employee_directory.active
    @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
    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/roles/payable.rb
    module Payable
    module PayRoll::Payable
    def pay_date?(date)
    # ...
    end
    @@ -46,9 +44,12 @@ def send_pay(pay_check)
    ## 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 controlle
    class PayDayController < ApplicationController
    def create
    PayRoll::PayDayService.new.execute
    redirect_to :back, :notice => "Pay day has been successfully completed"
    end
    end

  2. @nicholasjhenry nicholasjhenry revised this gist Dec 22, 2011. 1 changed file with 34 additions and 35 deletions.
    69 changes: 34 additions & 35 deletions your_application.rb
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,39 @@
    ## 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) }
    ## PayRoll gem

    # lib/pay_roll.rb
    module PayRoll
    class << self
    attr_accessor :employee_directory
    def config
    yield self
    end
    end
    end
    end

    def execute
    @employees.each do |e|
    if e.pay_date?(@date)
    pc = PayCheck.new(e.calculate_pay(@date))
    e.send_pay(pc)
    # lib/pay_roll/services/pay_day_service.rb
    module PayRoll
    class PayDayService
    def initialize(date=Date.now)
    @date = date

    @employees = PayRoll.employee_directory.active
    @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/pay_check_recipient.rb
    # Consider this the Role in DCI
    #
    module PayRoll::PayCheckRecipient
    # lib/pay_roll/roles/payable.rb
    module Payable
    def pay_date?(date)
    # ...
    end
    @@ -37,28 +43,21 @@ 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

    # config/initializers/pay_roll.rb
    PayRoll.config do |config|
    employee_directory = Employee
    end

    # models/employee.rb
    # The data in DCI
    #
    class Employee < ActiveRecord::Base
    scope :active, where(:active => true)
    end
  3. @nicholasjhenry nicholasjhenry revised this gist Dec 22, 2011. No changes.
  4. @nicholasjhenry nicholasjhenry revised this gist Oct 27, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion your_application.rb
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    # lib/pay_roll/pay_day_service.rb
    # Consider this Use Case as the Context in DCI
    #
    module PayRoll::PayDayService
    class PayRoll::PayDayService
    def initialize(date=Date.today)
    @date = date

  5. @nicholasjhenry nicholasjhenry revised this gist Oct 26, 2011. 1 changed file with 16 additions and 28 deletions.
    44 changes: 16 additions & 28 deletions your_application.rb
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,17 @@
    ## PayRoll gem

    # lib/pay_roll.rb
    module PayRoll
    class << self
    attr_accessor :database
    def config
    yield self
    end
    end
    end
    end
    ## PayRoll Application Gem

    # lib/pay_roll/pay_day_service.rb
    # Consider this Use Case as the Context in DCI
    #
    module PayRoll::PayDayService
    def initialize(date=Date.today)
    @date = date

    @employees ||= PayRoll.database.find_employees
    @employees.each { |e| e.extend(Payable) }
    # 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
    @@ -30,8 +24,10 @@ def execute
    end
    end

    # lib/pay_roll/payable.rb
    module PayRoll::Payable
    # lib/pay_roll/pay_check_recipient.rb
    # Consider this the Role in DCI
    #
    module PayRoll::PayCheckRecipient
    def pay_date?(date)
    # ...
    end
    @@ -42,6 +38,7 @@ def send_pay(pay_check)
    end

    # lib/pay_roll/pay_check.rb
    #
    class PayRoll::PayCheck
    # ...
    end
    @@ -51,26 +48,17 @@ class PayRoll::PayCheck
    # 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

    # config/initializers/pay_roll.rb
    PayRoll.config do |config|
    database = PayRollDatabase.new
    end

    # models/pay_roll_database.rb
    class PayRollDatabase
    def find_employees
    Employee.active
    end
    end

    # models/employee.rb
    # The data in DCI
    #
    class Employee < ActiveRecord::Base
    scope :active, where(:active => true)
    end
  6. @nicholasjhenry nicholasjhenry revised this gist Oct 24, 2011. 1 changed file with 18 additions and 15 deletions.
    33 changes: 18 additions & 15 deletions your_application.rb
    Original file line number Diff line number Diff line change
    @@ -12,28 +12,26 @@ def config
    end

    # lib/pay_roll/pay_day_service.rb
    module PayRoll
    class PayDayService
    def initialize(date=Date.today, options={})
    @date = date

    @employees ||= PayRoll.database.find_employees
    @employees.each { |e| e.extend(Payable) }
    end
    module PayRoll::PayDayService
    def initialize(date=Date.today)
    @date = date

    @employees ||= PayRoll.database.find_employees
    @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
    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/payable.rb
    module Payable
    module PayRoll::Payable
    def pay_date?(date)
    # ...
    end
    @@ -43,6 +41,11 @@ 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
  7. @nicholasjhenry nicholasjhenry revised this gist Oct 23, 2011. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions your_application.rb
    Original file line number Diff line number Diff line change
    @@ -57,11 +57,11 @@ def create

    # config/initializers/pay_roll.rb
    PayRoll.config do |config|
    database = Database.new
    database = PayRollDatabase.new
    end

    # lib/database.rb
    class Database
    # models/pay_roll_database.rb
    class PayRollDatabase
    def find_employees
    Employee.active
    end
  8. @nicholasjhenry nicholasjhenry revised this gist Oct 23, 2011. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions your_application.rb
    Original file line number Diff line number Diff line change
    @@ -3,22 +3,22 @@
    # lib/pay_roll.rb
    module PayRoll
    class << self
    attr_accessor :employee_directory
    attr_accessor :database
    def config
    yield self
    end
    end
    end
    end

    # lib/pay_roll/services/pay_day_service.rb
    # lib/pay_roll/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) }
    @employees ||= PayRoll.database.find_employees
    @employees.each { |e| e.extend(Payable) }
    end

    def execute
    @@ -32,7 +32,7 @@ def execute
    end
    end

    # lib/pay_roll/roles/payable.rb
    # lib/pay_roll/payable.rb
    module Payable
    def pay_date?(date)
    # ...
    @@ -57,12 +57,12 @@ def create

    # config/initializers/pay_roll.rb
    PayRoll.config do |config|
    employee_directory = EmployeeDirectory.new # alternatively just use Employee model
    database = Database.new
    end

    # models/employee_directory.rb
    class EmployeeDirectory
    def active
    # lib/database.rb
    class Database
    def find_employees
    Employee.active
    end
    end
  9. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions your_application.rb
    Original file line number Diff line number Diff line change
    @@ -46,6 +46,8 @@ def send_pay(pay_check)
    ## 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
  10. @nicholasjhenry nicholasjhenry renamed this gist Oct 22, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -49,6 +49,7 @@ def send_pay(pay_check)
    class PayDayController < ApplicationController
    def create
    PayRoll::PayDayService.new.execute
    redirect_to :back, :notice => "Pay day has been successfully completed"
    end
    end

  12. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ def config
    # lib/pay_roll/services/pay_day_service.rb
    module PayRoll
    class PayDayService
    def initialize(date=Date.now, options={})
    def initialize(date=Date.today, options={})
    @date = date

    options[:employees] ||= PayRoll.employee_directory.active
  13. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ def initialize(date=Date.now, options={})
    @date = date

    options[:employees] ||= PayRoll.employee_directory.active
    @employees = options[:employees].each { |e| e.extend(PayableEmployee) }
    @employees = options[:employees].each { |e| e.extend(Payable) }
    end

    def execute
    @@ -32,8 +32,8 @@ def execute
    end
    end

    # lib/pay_roll/roles/payable_employee.rb
    module PayableEmployee
    # lib/pay_roll/roles/payable.rb
    module Payable
    def pay_date?(date)
    # ...
    end
  14. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,7 @@ def create

    # config/initializers/pay_roll.rb
    PayRoll.config do |config|
    employee_directory = EmployeeDirectory.new
    employee_directory = EmployeeDirectory.new # alternatively just use Employee model
    end

    # models/employee_directory.rb
  15. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 43 additions and 13 deletions.
    56 changes: 43 additions & 13 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,24 @@
    # payroll gem
    ## PayRoll gem

    # lib/pay_roll.rb
    module PayRoll
    class PayDayService < Service
    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.now, options={})
    @date = date

    options[:employees] ||= datastore.find_all_active_employees
    @employees = options[:employees].each{|e| e.extend(Employee) }
    options[:employees] ||= PayRoll.employee_directory.active
    @employees = options[:employees].each { |e| e.extend(PayableEmployee) }
    end

    def execute
    @@ -18,23 +30,41 @@ def execute
    end
    end
    end
    end

    module PayableEmployee
    def pay_date?(date)
    # ...
    end
    # lib/pay_roll/roles/payable_employee.rb
    module PayableEmployee
    def pay_date?(date)
    # ...
    end

    def send_pay(pay_check)
    # ...
    end
    def send_pay(pay_check)
    # ...
    end
    end

    # Rails application, pay_day_controller.rb
    ## Rails application

    # app/controllers/pay_day_controller.rb
    class PayDayController < ApplicationController
    def create
    PayRoll::PayDayService.new(Employee.all).execute
    PayRoll::PayDayService.new.execute
    end
    end

    # config/initializers/pay_roll.rb
    PayRoll.config do |config|
    employee_directory = EmployeeDirectory.new
    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
  16. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # payroll gem

    module PayRoll
    class PayDayService < Service
    def initialize(date=Date.now, options={})
    @@ -28,4 +30,11 @@ def send_pay(pay_check)
    end
    end

    PayRoll::PayDayService.new(Employee.all).execute
    # Rails application, pay_day_controller.rb

    class PayDayController < ApplicationController
    def create
    PayRoll::PayDayService.new(Employee.all).execute
    end
    end

  17. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,10 @@
    module PayRoll
    class PayDayService < Service
    def initialize(date=Date.now, employees=datastore.find_all_employees)
    def initialize(date=Date.now, options={})
    @date = date
    @employees = employees.each{|e| e.extend(Employee) }

    options[:employees] ||= datastore.find_all_active_employees
    @employees = options[:employees].each{|e| e.extend(Employee) }
    end

    def execute
  18. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 6 additions and 9 deletions.
    15 changes: 6 additions & 9 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    module PayRoll
    class PayDayService
    def initialize(employees_attributes, date=Date.now)
    @employees = employees_attributes.collect {|ea| Employee.new(ea)}
    class PayDayService < Service
    def initialize(date=Date.now, employees=datastore.find_all_employees)
    @date = date
    @employees = employees.each{|e| e.extend(Employee) }
    end

    def execute
    @@ -14,13 +15,9 @@ def execute
    end
    end

    class Employee
    def initialize(attributes)
    @attributes = attributes
    end

    module PayableEmployee
    def pay_date?(date)
    # ... @attributes.started_on_date
    # ...
    end

    def send_pay(pay_check)
  19. @nicholasjhenry nicholasjhenry revised this gist Oct 22, 2011. 1 changed file with 28 additions and 10 deletions.
    38 changes: 28 additions & 10 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,32 @@
    class PayDayService
    def initialize(employees, date)
    @employees, @date = employees, date
    end
    module PayRoll
    class PayDayService
    def initialize(employees_attributes, date=Date.now)
    @employees = employees_attributes.collect {|ea| Employee.new(ea)}
    end

    def execute
    @employees.each do |e|
    if e.pay_date?(@date)
    pc = PayCheck.new(e.calculate_pay(@date))
    e.send_pay(pc)
    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

    class Employee
    def initialize(attributes)
    @attributes = attributes
    end

    def pay_date?(date)
    # ... @attributes.started_on_date
    end

    def send_pay(pay_check)
    # ...
    end
    end
    end

    PayRoll::PayDayService.new(Employee.all).execute
  20. @nicholasjhenry nicholasjhenry created this gist Oct 22, 2011.
    14 changes: 14 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    class PayDayService
    def initialize(employees, date)
    @employees, @date = employees, date
    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