Class: Blackcal::WeekdayRange
- Inherits:
-
Object
- Object
- Blackcal::WeekdayRange
- 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
-
#weekdays ⇒ Array<Symbol>
(also: #to_a)
readonly
Weekdays in range.
Instance Method Summary collapse
-
#cover?(timestamp) ⇒ Boolean
Returns true if it covers timestamp.
-
#each(&block) ⇒ Object
Iterate over range.
-
#initialize(weekdays) ⇒ WeekdayRange
constructor
Initialize weekday range.
Constructor Details
#initialize(weekdays) ⇒ WeekdayRange
Initialize weekday range
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
#weekdays ⇒ Array<Symbol> (readonly) Also known as: to_a
Returns 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
43 44 45 46 47 48 49 |
# File 'lib/blackcal/range/weekday_range.rb', line 43 def cover?() return false if @weekdays.nil? || @weekdays.empty? weekdays.any? do |weekday| WEEKDAY_MAP.fetch(weekday) == .wday end end |
#each(&block) ⇒ Object
Iterate over range
56 57 58 |
# File 'lib/blackcal/range/weekday_range.rb', line 56 def each(&block) to_a.each(&block) end |