Class: Montrose::Day
- Inherits:
-
Object
show all
- Extended by:
- Utils
- Defined in:
- lib/montrose/day.rb
Constant Summary
collapse
- NAMES =
::Date::DAYNAMES
- TWO_LETTER_ABBREVIATIONS =
%w[SU MO TU WE TH FR SA].freeze
- THREE_LETTER_ABBREVIATIONS =
%w[SUN MON TUE WED THU FRI SAT]
- NUMBERS =
NAMES.map.with_index { |_n, i| i.to_s }
- ICAL_MATCH =
/(?<ordinal>[+-]?\d+)?(?<day>[A-Z]{2})/
Constants included
from Utils
Utils::MAX_DAYS_IN_MONTH, Utils::MAX_DAYS_IN_YEAR, Utils::MAX_HOURS_IN_DAY, Utils::MAX_WEEKS_IN_YEAR
Class Method Summary
collapse
Methods included from Utils
as_date, as_time, current_time, days_in_month, days_in_year, normalize_time, parse_time, to_index
Class Method Details
.map_arg(arg, &block) ⇒ Object
44
45
46
47
48
|
# File 'lib/montrose/day.rb', line 44
def map_arg(arg, &block)
return nil unless arg.present?
Array(arg).map(&block)
end
|
.names ⇒ Object
50
51
52
|
# File 'lib/montrose/day.rb', line 50
def names
NAMES
end
|
.number(name) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# 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) ⇒ Object
69
70
71
72
|
# 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 ⇒ Object
74
75
76
|
# File 'lib/montrose/day.rb', line 74
def numbers
NUMBERS
end
|
.parse(arg) ⇒ Object
13
14
15
16
17
18
19
20
21
22
|
# 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) ⇒ Object
24
25
26
27
28
29
30
31
|
# 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) ⇒ Object
37
38
39
40
41
42
|
# 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) ⇒ Object
33
34
35
|
# File 'lib/montrose/day.rb', line 33
def parse_value(value)
parse_ical(value) || [number!(value), nil]
end
|