Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.