aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sale.rb
blob: 057927c24570a6cf80ba0a55bdbd1caaf40239ec (plain) (blame)
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
module SalesReporter
  class Sale
    attr_reader :date, :amount, :quantity, :country

    def initialize(date_, amount, quantity, country)
      @date = sanitize_date(date_)
      raise TypeError('sanitize failed') unless @date.is_a? Date
      @amount = amount
      @quantity = quantity
      @country = country
    end

    private

    def sanitize_date(d)
      case d.class.to_s
      when "String"
        return Date.parse(d)
      when "Date"
        return d
      else
        raise TypeError.new('Expected date argument to be of type Date or String')
      end
    end
  end
end