Class: Blackcal::MonthRange

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

Overview

Month range

Constant Summary collapse

MONTH_MAP =

Map month name to number

{
  january: 1,
  february: 2,
  march: 3,
  april: 4,
  may: 5,
  june: 6,
  july: 7,
  august: 8,
  september: 9,
  october: 10,
  november: 11,
  december: 12,
}.freeze
MONTH_INVERT_MAP =

Map month number to name

MONTH_MAP.invert.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(months) ⇒ MonthRange

Initialize month range

Examples:

MonthRange.new(:january)
MonthRange.new([:december, :january])

Parameters:

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


36
37
38
39
40
41
42
43
44
# File 'lib/blackcal/range/month_range.rb', line 36

def initialize(months)
  return unless months

  @months = Array(months).map do |month|
    next MONTH_INVERT_MAP.fetch(month) if month.is_a?(Integer)

    month.downcase.to_sym
  end
end

Instance Attribute Details

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

Returns months in range.

Returns:

  • (Array<Symbol>)

    months in range



28
29
30
# File 'lib/blackcal/range/month_range.rb', line 28

def months
  @months
end

Instance Method Details

#cover?(timestamp) ⇒ Boolean

Returns true if it covers timestamp

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/blackcal/range/month_range.rb', line 48

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

  months.any? do |month|
    MONTH_MAP.fetch(month) == timestamp.month
  end
end

#each(&block) ⇒ Object

Iterate over range

See Also:



61
62
63
# File 'lib/blackcal/range/month_range.rb', line 61

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