class Montrose::Day

Constants

ICAL_MATCH
NAMES
NUMBERS
THREE_LETTER_ABBREVIATIONS
TWO_LETTER_ABBREVIATIONS

Public Class Methods

abbreviations() click to toggle source
# File lib/montrose/day.rb, line 78
def abbreviations
  TWO_LETTER_ABBREVIATIONS + THREE_LETTER_ABBREVIATIONS
end
map_arg(arg, &block) click to toggle source
# File lib/montrose/day.rb, line 44
def map_arg(arg, &block)
  return nil unless arg.present?

  Array(arg).map(&block)
end
names() click to toggle source
# File lib/montrose/day.rb, line 50
def names
  NAMES
end
number(name) click to toggle source
# File lib/montrose/day.rb, line 54
def number(name)
  case name
  when 0..6
    name
  when Symbol, String
    string = name.to_s.downcase
    NAMES.index(string.titleize) ||
      TWO_LETTER_ABBREVIATIONS.index(string.upcase) ||
      THREE_LETTER_ABBREVIATIONS.index(string.upcase) ||
      number(to_index(string))
  when Array
    number name.first
  end
end
number!(name) click to toggle source
# File lib/montrose/day.rb, line 69
def number!(name)
  number(name) || raise(ConfigurationError,
    "Did not recognize day #{name}, must be one of #{(names + abbreviations + numbers).inspect}")
end
numbers() click to toggle source
# File lib/montrose/day.rb, line 74
def numbers
  NUMBERS
end
parse(arg) click to toggle source
# File lib/montrose/day.rb, line 13
def parse(arg)
  case arg
  when Hash
    parse_entries(arg.entries)
  when String
    parse(arg.split(","))
  else
    parse_entries(map_arg(arg) { |value| parse_value(value) })
  end
end
parse_entries(entries) click to toggle source
# File lib/montrose/day.rb, line 24
def parse_entries(entries)
  hash = Hash.new { |h, k| h[k] = [] }
  result = entries.each_with_object(hash) { |(k, v), hash|
    index = number!(k)
    hash[index] = hash[index] + [*v]
  }
  result.values.all?(&:empty?) ? result.keys : result
end
parse_ical(value) click to toggle source
# File lib/montrose/day.rb, line 37
def parse_ical(value)
  (match = ICAL_MATCH.match(value.to_s)) || (return nil)
  index = number!(match[:day])
  ordinal = match[:ordinal]&.to_i
  [index, ordinal]
end
parse_value(value) click to toggle source
# File lib/montrose/day.rb, line 33
def parse_value(value)
  parse_ical(value) || [number!(value), nil]
end