/
config.ex
242 lines (200 loc) · 7.06 KB
/
config.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
defmodule Cldr.Calendar.Config do
@moduledoc """
Defines the configuration for a calendar.
See `Cldr.Calendar.new/3` for usage details.
"""
defstruct calendar: nil,
# A default backend for this
# calendar
cldr_backend: nil,
# Each quarter has three
# 'months` each of 13 weeks
# in either of a 4,4,5; 4,5,4
# of 5,4,4 layout
weeks_in_month: [4, 4, 5],
# Indicates if the anchor
# represents the beginning
# of the year or the end
begins_or_ends: :begins,
# Calendar begins on the
# :first, :last or :nearest
first_or_last: :first,
# Year begins on this day
# Use :first to mean the day
# day of the week on which the
# first day of the year occurs
# The functions `Cldr.Calendar.monday()`
# etc can be used
day_of_week: 1,
# Year begins in this Gregorian month
month_of_year: 1,
# The year of the last_day or first_day
# is either the year with the :majority
# of months or the :beginning year
# or :ending year
year: :majority,
# First week has at least
# this many days in current
# year
min_days_in_first_week: 1
@typedoc """
Defines the struct type for a calendar configuration
"""
@type t() :: %__MODULE__{
calendar: atom(),
cldr_backend: Cldr.backend() | nil,
weeks_in_month: list(pos_integer()),
begins_or_ends: :begins | :ends,
first_or_last: :first | :last,
day_of_week: Cldr.Calendar.day_of_week(),
month_of_year: pos_integer(),
year: :majority | :starts | :ends,
min_days_in_first_week: 1..7
}
@doc false
def extract_options(options) do
invalidate_old_options!(options)
detect_invalid_options!(options)
default_backend = Application.get_env(Cldr.Config.app_name(), :default_backend)
backend = Keyword.get(options, :backend, default_backend)
backend = if backend && Cldr.Code.ensure_compiled?(backend), do: backend, else: nil
%__MODULE__{
calendar: Keyword.get(options, :calendar),
cldr_backend: backend,
min_days_in_first_week: min_days_for_locale(backend, options),
day_of_week: first_day_for_locale(backend, options),
year: Keyword.get(options, :year, :majority),
month_of_year: Keyword.get(options, :month_of_year, 1),
first_or_last: Keyword.get(options, :first_or_last, :first),
begins_or_ends: Keyword.get(options, :begins_or_ends, :begins),
weeks_in_month: Keyword.get(options, :weeks_in_month, [4, 5, 4])
}
end
def min_days_for_locale(nil, options) do
default =
if locale = Keyword.get(options, :locale) do
Cldr.Calendar.min_days_for_locale(locale)
else
1
end
Keyword.get(options, :min_days_in_first_week, default)
end
def min_days_for_locale(backend, options) do
locale = Keyword.get(options, :locale, backend.get_locale())
Keyword.get(options, :min_days_in_first_week, Cldr.Calendar.min_days_for_locale(locale))
end
def first_day_for_locale(nil, options) do
default =
if locale = Keyword.get(options, :locale) do
Cldr.Calendar.first_day_for_locale(locale)
else
1
end
Keyword.get(options, :day_of_week, default)
end
def first_day_for_locale(backend, options) do
locale = Keyword.get(options, :locale, backend.get_locale())
Keyword.get(options, :day_of_week, Cldr.Calendar.first_day_for_locale(locale))
end
@valid_weeks_in_month [[4, 4, 5], [4, 5, 4], [5, 4, 4]]
@valid_year [:majority, :beginning, :ending]
@doc false
def validate_config(config, calendar_type) do
with :ok <-
validate_day(config, calendar_type),
:ok <-
assert(config.month_of_year in 1..12, month_error(config.month_of_year)),
:ok <-
assert(config.year in @valid_year, year_error(config.year)),
:ok <-
assert(
config.min_days_in_first_week in 1..7,
min_days_for_locale_error(config.min_days_in_first_week)
),
:ok <-
assert(
config.first_or_last in [:first, :last],
first_or_last_error(config.first_or_last)
),
:ok <-
assert(
config.begins_or_ends in [:begins, :ends],
begins_or_ends_error(config.begins_or_ends)
),
:ok <-
assert(
config.weeks_in_month in @valid_weeks_in_month,
weeks_in_month_error(config.weeks_in_month)
) do
{:ok, config}
end
end
@doc false
def validate_config!(config, calendar_type) do
case validate_config(config, calendar_type) do
{:ok, config} -> config
{:error, message} -> raise ArgumentError, message
end
end
defp invalidate_old_options!(options) do
if options[:day],
do: raise(ArgumentError, "Option :day is replaced with :day_of_week")
if options[:month],
do: raise(ArgumentError, "Option :month is replaced with :month_of_year")
if options[:min_days],
do: raise(ArgumentError, "Option :min_days is replaced with :min_days_in_first_week")
end
defp valid_options do
%__MODULE__{}
|> Map.delete(:__struct__)
|> Map.keys()
|> List.insert_at(0, :locale)
end
defp detect_invalid_options!(options) do
case Enum.filter(options, fn {key, _} -> key not in valid_options() end) do
[] ->
options
invalid_options ->
raise ArgumentError,
"Invalid options #{inspect(invalid_options)} found. Valid options are #{
inspect(valid_options())
}"
end
end
defp validate_day(config, :week) do
assert(config.day_of_week in 1..7, day_error(config.day_of_week))
end
defp validate_day(config, :month) do
assert(
config.day_of_week in 1..7 or config.day_of_week == :first,
day_error(config.day_of_week)
)
end
defp assert(true, _) do
:ok
end
defp assert(false, message) do
{:error, message}
end
defp day_error(day) do
":day_of_week must be in the range 1..7. Found #{inspect(day)}."
end
defp month_error(month) do
":month_of_year must be in the range 1..12. Found #{inspect(month)}."
end
defp year_error(year) do
":year must be either :beginning, :ending or :majority. Found #{inspect(year)}."
end
defp min_days_for_locale_error(min_days) do
":min_days_in_first_week must be in the range 1..7. Found #{inspect(min_days)}."
end
defp first_or_last_error(first_or_last) do
":first_or_last must be :first or :last. Found #{inspect(first_or_last)}."
end
defp begins_or_ends_error(begins_or_ends) do
":begins_or_ends must be :begins or :ends. Found #{inspect(begins_or_ends)}."
end
defp weeks_in_month_error(weeks_in_month) do
":weeks_in_month must be [4,4,5], [4,5,4] or [5,4,4]. Found #{inspect(weeks_in_month)}"
end
end