aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sale.rb
blob: 17c6f181952fd92d06e0b1529ab4d69751c71b7e (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
27
module SalesReporter
  class Sale
    attr_reader :date, :amount, :quantity, :country, :shop

    def initialize(date_, amount, quantity, country, shop)
      @date = sanitize_date(date_)
      raise TypeError('sanitize failed') unless @date.is_a? Date
      @amount = amount
      @quantity = quantity
      @country = country
      @shop = shop
    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