Class: Syukujitsu::Calendar
- Inherits:
-
Object
- Object
- Syukujitsu::Calendar
- Includes:
- Enumerable
- Defined in:
- lib/syukujitsu/calendar.rb
Constant Summary collapse
- CSV_PATH =
File.("../../holidays.csv", __dir__)
Class Method Summary collapse
Instance Method Summary collapse
- #between(start_date, end_date) ⇒ Object
- #each ⇒ Object
- #holiday?(date) ⇒ Boolean
-
#initialize(entities) ⇒ Calendar
constructor
A new instance of Calendar.
- #name(date) ⇒ Object
- #on(date) ⇒ Object
Constructor Details
#initialize(entities) ⇒ Calendar
Returns a new instance of Calendar.
16 17 18 19 |
# File 'lib/syukujitsu/calendar.rb', line 16 def initialize(entities) @entities = entities.dup.freeze @dates = @entities.keys.sort.freeze end |
Class Method Details
Instance Method Details
#between(start_date, end_date) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/syukujitsu/calendar.rb', line 37 def between(start_date, end_date) s = normalize_date(start_date) e = normalize_date(end_date) return [] if s > e left = @dates.bsearch_index { |d| d >= s } return [] if left.nil? right = @dates.bsearch_index { |d| d > e } selected_dates = right.nil? ? @dates[left..] : @dates[left...right] selected_dates.map { |date| @entities[date] } end |
#each ⇒ Object
21 22 23 |
# File 'lib/syukujitsu/calendar.rb', line 21 def each(&) @entities.each_value(&) end |
#holiday?(date) ⇒ Boolean
25 26 27 |
# File 'lib/syukujitsu/calendar.rb', line 25 def holiday?(date) @entities.key?(normalize_date(date)) end |
#name(date) ⇒ Object
33 34 35 |
# File 'lib/syukujitsu/calendar.rb', line 33 def name(date) on(date)&.name end |
#on(date) ⇒ Object
29 30 31 |
# File 'lib/syukujitsu/calendar.rb', line 29 def on(date) @entities[normalize_date(date)] end |