Skip to content

Instantly share code, notes, and snippets.

@msuzoagu
Forked from oliviermilla/CORP.rb
Created January 22, 2017 22:57
Show Gist options
  • Select an option

  • Save msuzoagu/b9466e42e4bb0a76bd424516e77883b0 to your computer and use it in GitHub Desktop.

Select an option

Save msuzoagu/b9466e42e4bb0a76bd424516e77883b0 to your computer and use it in GitHub Desktop.
class PurchasePower
def self.base
500
end
def process_request(request)
raise NotImplementedError
end
private
def next
if successor.nil?
successor.process_request(request)
else
yield block if block_given?
end
end
attr_accessor :successor
end
class Manager < PurchasePower
ALLOWABLE = 10 * base
def process_request(request)
if request.amount < ALLOWABLE
puts("Manager will approve $#{request.amount}")
return
end
next
end
end
class Director < PurchasePower
ALLOWABLE = 20 * base
def process_request(request)
if request.amount < ALLOWABLE
puts("Director will approve $#{request.amount}")
return
end
next
end
end
class VicePresident < PurchasePower
ALLOWABLE = 40 * base
def process_request(request)
if request.amount < ALLOWABLE
puts("VicePresident will approve $#{request.amount}")
return
end
next
end
end
class President < PurchasePower
ALLOWABLE = 60 * base
def process_request(request)
if request.amount < ALLOWABLE
puts("President will approve $#{request.amount}")
return
end
next{ puts "Your request for $#{request.amount} needs a board meeting!" }
end
end
class PurchaseRequest
def initialize(number, amount, purpose)
@number = number
@amount = amount
@purpose = purpose
end
attr_reader :amount
end
class Approvers
def initialize(*approvers)
@approvers = approvers.map(&:new)
end
def <<(approver)
@approvers << approver
end
def -(approver)
@approvers.delete approver
end
attr_reader :approvers
end
class CheckAuthority
def initialize(approvers)
@approvers = approvers
@starting_approver = build_chains.first
end
def checking
loop do
$stdout.flush
puts "Enter the amount to check who should approve your expenditure."
puts ">>"
@starting_approver.process_request PurchaseRequest.new(0, gets.to_i, 'General')
end
end
private
def build_chains
@approvers.each_cons(2).map { |_prev, _next| _prev.send :successor=, _next }
end
end
approvers = Approvers.new(PurchasePower.descendants) #Warning this is rails dependent
CheckAuthority.new(approvers).checking
Arup-iMac:arup_ruby shreyas$ ruby a.rb
Enter the amount to check who should approve your expenditure.
>>
1200
Director will approve $1200
Enter the amount to check who should approve your expenditure.
>>
2000
Director will approve $2000
Enter the amount to check who should approve your expenditure.
>>
^[[A^[[B
Director will approve $0
Enter the amount to check who should approve your expenditure.
>>
3000
Director will approve $3000
Enter the amount to check who should approve your expenditure.
>>
40000
Your request for $40000 needs a board meeting!
Enter the amount to check who should approve your expenditure.
>>
4000
Director will approve $4000
Enter the amount to check who should approve your expenditure.
>>
30000
Your request for $30000 needs a board meeting!
Enter the amount to check who should approve your expenditure.
>>
10000
VicePresident will approve $10000
Enter the amount to check who should approve your expenditure.
>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment