summaryrefslogtreecommitdiff
path: root/lib/grant.rb
blob: 10f89f48448af5e8f9572401929b6259415d501b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Grant
  include Enumerable
  attr_reader :issued_at

  def initialize(value_of_grant, share_price, date = Clock.today)
    number_of_units = value_of_grant / share_price
    @units = Array.new(number_of_units) { Unit.new }
    @issued_at = date
  end

  def vest_at(price, portion)
    number_of_units_to_vest = portion.to_f * @units.count
    unvested_units.take(number_of_units_to_vest).each do |unit|
      unit.vest_at(price)
    end
  end

  def each
    @units.each { |unit| yield unit }
  end

  def value_of(units, price)
    units.inject(0.00.dollars) do |memo, unit|
      memo + (unit * price)
    end
  end

  private

  def unvested_units
    @units - vested_units
  end

  def vested_units
    @units.find_all { |x| x.vested? }
  end
end