Class: SchoolDays::Calculator
- Inherits:
-
Object
- Object
- SchoolDays::Calculator
- Defined in:
- lib/school_days/calculator.rb
Instance Method Summary collapse
- #after(time = Time.now) ⇒ Object (also: #from_now, #since)
- #before(time = Time.now) ⇒ Object (also: #until)
-
#initialize(days) ⇒ Calculator
constructor
A new instance of Calculator.
-
#is_in_school_year?(date) ⇒ Boolean
another semi-private method.
-
#school_session_for_date(date) ⇒ Object
a new method, more of a helper for this API then an actual public method…
Constructor Details
#initialize(days) ⇒ Calculator
Returns a new instance of Calculator.
4 5 6 |
# File 'lib/school_days/calculator.rb', line 4 def initialize(days) @days = days end |
Instance Method Details
#after(time = Time.now) ⇒ Object Also known as: from_now, since
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/school_days/calculator.rb', line 8 def after(time = Time.now) # example: 2.school_days.after(tuesday) date = time date = time.to_date if time.is_a?(Time) @days.times do begin date = date + 1 raise SchoolDays::DateNotInSchoolCalendar unless is_in_school_year?(date) # (if we are not in the school year at all, stop calculating, because # once we go outside we'll never find another school day. WD-rpw 01-26-2011) end until date.school_day? end date end |
#before(time = Time.now) ⇒ Object Also known as: until
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/school_days/calculator.rb', line 27 def before(time = Time.now) # example: 2.school_days.after(tuesday) date = time date = time.to_date if time.is_a?(Time) @days.times do begin date = date - 1 raise SchoolDays::DateNotInSchoolCalendar unless is_in_school_year?(date) # (if we are not in the school year at all, stop calculating, because # once we go outside we'll never find another school day. WD-rpw 01-26-2011) end until date.school_day? end date end |
#is_in_school_year?(date) ⇒ Boolean
another semi-private method. TODO: it seems useful to expose this, but how?
54 55 56 57 58 59 60 |
# File 'lib/school_days/calculator.rb', line 54 def is_in_school_year?(date) # included exceptional days can be outside the school year return true if SchoolDays.config.included_day_exceptions.include? date # ok, now check ranges SchoolDays.config.school_year_start < date && date < SchoolDays.config.school_year_end end |
#school_session_for_date(date) ⇒ Object
a new method, more of a helper for this API then an actual public method… however, this will return the session a date is associated with, or nil if this falls totally outside the known school year
47 48 49 50 51 |
# File 'lib/school_days/calculator.rb', line 47 def school_session_for_date(date) SchoolDays.config.school_sessions.detect do |school_session| school_session[:start_date] < date && date < school_session[:end_date] end end |