Class: Blackcal::WeekdayRange

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/blackcal/range/weekday_range.rb

Overview

Weekday range

Constant Summary collapse

WEEKDAY_MAP =

Map weekday name to number

{
  sunday: 0,
  monday: 1,
  tuesday: 2,
  wednesday: 3,
  thursday: 4,
  friday: 5,
  saturday: 6,
}.freeze
WEEKDAY_INVERT_MAP =

Map weekday number to name

WEEKDAY_MAP.invert.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(weekdays) ⇒ WeekdayRange

Initialize weekday range

Examples:

WeekdayRange.new(:monday)
WeekdayRange.new([:monday, :thursday])

Parameters:

  • weekdays (Array<String>, Array<Symbol>, Array<Integer>, String, Symbol, Integer, nil)


31
32
33
34
35
36
37
38
39
# File 'lib/blackcal/range/weekday_range.rb', line 31

def initialize(weekdays)
  return unless weekdays

  @weekdays = Array(weekdays).map do |week|
    next WEEKDAY_INVERT_MAP.fetch(week) if week.is_a?(Integer)

    week.downcase.to_sym
  end
end

Instance Attribute Details

#weekdaysArray<Symbol> (readonly) Also known as: to_a

Returns weekdays in range.

Returns:

  • (Array<Symbol>)

    weekdays in range



23
24
25
# File 'lib/blackcal/range/weekday_range.rb', line 23

def weekdays
  @weekdays
end

Instance Method Details

#cover?(timestamp) ⇒ Boolean

Returns true if it covers timestamp

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/blackcal/range/weekday_range.rb', line 43

def cover?(timestamp)
  return false if @weekdays.nil? || @weekdays.empty?

  weekdays.any? do |weekday|
    WEEKDAY_MAP.fetch(weekday) == timestamp.wday
  end
end

#each(&block) ⇒ Object

Iterate over range

See Also:



56
57
58
# File 'lib/blackcal/range/weekday_range.rb', line 56

def each(&block)
  to_a.each(&block)
end