Skip to content

Instantly share code, notes, and snippets.

@kellysutton
Created September 11, 2017 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellysutton/add84a41ed3560c683d47fe5bf11991f to your computer and use it in GitHub Desktop.
Save kellysutton/add84a41ed3560c683d47fe5bf11991f to your computer and use it in GitHub Desktop.
Embracing Functional Programming in Ruby
def taxes
PayrollCalculator::Taxes.calculate(
@payroll.only_pay_and_location_data
)
end
def taxes
PayrollCalculator::Taxes.calculate(@payroll)
end
def paystubs
calculate_paystubs(taxes, ...)
end
def debits
calculate_debits(taxes, ...)
end
def taxes
@taxes ||= calculate_taxes(@payroll)
end
def taxes
federal_taxes(@payroll) +
california_taxes(@payroll) +
new_york_taxes(@payroll) +
local_california_taxes(@payroll) +
local_new_york_taxes(@payroll)
end
# app/models/payroll.rb
class Payroll < ActiveRecord::Base
end
# app/services/payroll_calculator/payroll.rb
class PayrollCalculator::Payroll < ValueObject
end
class PayrollCalculator
def self.calculate(payroll)
new(payroll).calculate
end
def initialize(payroll)
@payroll = payroll
end
private_class_method :new
def calculate
PayrollResult.new(
payroll: payroll,
paystubs: paystubs,
taxes: taxes,
debits: debits
)
end
def paystubs
# ...
end
def taxes
# ...
end
def debits
# ...
end
end
def taxes
federal_taxes(@payroll) +
california_taxes(@payroll) +
local_taxes(@payroll)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment