Class: Feriados::Calendar
- Inherits:
-
Object
- Object
- Feriados::Calendar
- Defined in:
- lib/feriados/calendar.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add(rule) ⇒ Object
- #eql?(other) ⇒ Boolean
- #holiday?(date) ⇒ Boolean
- #holiday_name(date) ⇒ Object
- #holidays_between(start_date, end_date) ⇒ Object
- #holidays_in_year(year) ⇒ Object
-
#initialize ⇒ Calendar
constructor
A new instance of Calendar.
- #load(rules) ⇒ Object
- #next_holiday(from_date = Date.today) ⇒ Object
- #remove(rule) ⇒ Object
Constructor Details
#initialize ⇒ Calendar
Returns a new instance of Calendar.
6 7 8 |
# File 'lib/feriados/calendar.rb', line 6 def initialize @rules = Set.new end |
Instance Method Details
#==(other) ⇒ Object
75 76 77 |
# File 'lib/feriados/calendar.rb', line 75 def ==(other) eql?(other) end |
#add(rule) ⇒ Object
18 19 20 |
# File 'lib/feriados/calendar.rb', line 18 def add(rule) rules << rule end |
#eql?(other) ⇒ Boolean
71 72 73 |
# File 'lib/feriados/calendar.rb', line 71 def eql?(other) rules == other.rules end |
#holiday?(date) ⇒ Boolean
10 11 12 |
# File 'lib/feriados/calendar.rb', line 10 def holiday?(date) rules.any? { |rule| rule.holiday?(date) } end |
#holiday_name(date) ⇒ Object
14 15 16 |
# File 'lib/feriados/calendar.rb', line 14 def holiday_name(date) rules.find { |rule| rule.holiday?(date) }&.name end |
#holidays_between(start_date, end_date) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/feriados/calendar.rb', line 36 def holidays_between(start_date, end_date) holidays = [] current_date = start_date while current_date <= end_date if holiday?(current_date) holidays << { date: current_date, name: holiday_name(current_date) } end current_date += 1 end holidays end |
#holidays_in_year(year) ⇒ Object
30 31 32 33 34 |
# File 'lib/feriados/calendar.rb', line 30 def holidays_in_year(year) start_date = Date.new(year, 1, 1) end_date = Date.new(year, 12, 31) holidays_between(start_date, end_date) end |
#load(rules) ⇒ Object
26 27 28 |
# File 'lib/feriados/calendar.rb', line 26 def load(rules) Loader.new(rules, self).load end |
#next_holiday(from_date = Date.today) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/feriados/calendar.rb', line 53 def next_holiday(from_date = Date.today) # Search in the next 2 years to ensure finding at least one holiday end_search_date = Date.new(from_date.year + 2, 12, 31) current_date = from_date while current_date <= end_search_date if holiday?(current_date) return { date: current_date, name: holiday_name(current_date) } end current_date += 1 end nil # No holidays found in the range end |
#remove(rule) ⇒ Object
22 23 24 |
# File 'lib/feriados/calendar.rb', line 22 def remove(rule) rules.delete(rule) end |