Parcourir la source

Schueler#volljaehrig_bei?(datum) für historische Altersabfragen

hmt il y a 9 ans
Parent
commit
eaaf64e662
2 fichiers modifiés avec 11 ajouts et 0 suppressions
  1. 6 0
      lib/schild.rb
  2. 5 0
      specs/schueler_spec.rb

+ 6 - 0
lib/schild.rb

@@ -213,6 +213,12 @@ module SchildErweitert
       self.volljaehrig == "+"
     end
 
+    # gibt an, ob der Schüler zu einem Zeitpunkt *datum* volljährig war.
+    def volljaehrig_bei?(datum)
+      geb, datum = self.geburtsdatum.to_date, datum.to_date
+      (datum.year - geb.year - ((datum.month > geb.month || (datum.month == geb.month && datum.day >= geb.day)) ? 0 : 1)) >= 18
+    end
+
     def foto_vorhanden?
       self.foto_vorhanden == "+"
     end

+ 5 - 0
specs/schueler_spec.rb

@@ -138,6 +138,11 @@ describe Schueler do
       @sm.volljaehrig?.must_equal true
     end
 
+    it 'gibt an, ob Schüler im Vergelich zu *datum* volljährig ist' do
+      @sm.volljaehrig_bei?(@sm.halbjahr(2013,2).zeugnis_datum).must_equal false
+      Schueler[24].volljaehrig_bei?(Time.new).must_equal true
+    end
+
     it 'gibt zurück, ob ein Foto vorhanden ist' do
       @sm.foto_vorhanden?.must_equal false
     end