Class: Syukujitsu::Calendar

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/syukujitsu/calendar.rb

Constant Summary collapse

CSV_PATH =
File.expand_path("../../holidays.csv", __dir__)

Class Method Summary collapse

Instance Method Summary collapse

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

.load(csv_path: CSV_PATH, loader: CsvLoader.new) ⇒ Object



12
13
14
# File 'lib/syukujitsu/calendar.rb', line 12

def self.load(csv_path: CSV_PATH, loader: CsvLoader.new)
  new(loader.load(csv_path))
end

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

Returns:

  • (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