Sha256: c708c6490e7f54fe5ad96cddf56b1475776e63a921248a98f7d2c32db42eea7c

Contents?: true

Size: 900 Bytes

Versions: 2

Compression:

Stored size: 900 Bytes

Contents

# frozen_string_literal: true

module Blackcal
  # Weekday range
  class WeekdayRange
    WEEKDAY_MAP = {
      sunday: 0,
      monday: 1,
      tuesday: 2,
      wednesday: 3,
      thursday: 4,
      friday: 5,
      saturday: 6,
    }.freeze

    # @return [Array<Symbol>] weekdays in range
    attr_reader :weekdays

    # Initialize weekday range
    # @param [Array<String>, Array<Symbol>, String, Symbol, nil] weekdays
    # @example
    #   WeekdayRange.new(:monday)
    # @example
    #   WeekdayRange.new([:monday, :thursday])
    def initialize(weekdays)
      @weekdays = Array(weekdays).map(&:to_sym) if weekdays
    end

    # Returns true if it covers timestamp
    # @return [Boolean]
    def cover?(timestamp)
      return false if @weekdays.nil? || @weekdays.empty?

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blackcal-0.2.0 lib/blackcal/range/weekday_range.rb
blackcal-0.1.0 lib/blackcal/weekday_range.rb