aboutsummaryrefslogblamecommitdiffstats
path: root/lib/sale.rb
blob: 17c6f181952fd92d06e0b1529ab4d69751c71b7e (plain) (tree)
1
2
3
4
5
6
7
8
9

                    
                                                          
 
                                                          



                                                                
                        
                  















                                                                                  
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