Module: Timeliness::Definitions
- Defined in:
- lib/timeliness/definitions.rb
Constant Summary collapse
- US_FORMAT_REGEXP =
/\Am{1,2}[^m]/- FormatNotFound =
Class.new(StandardError)
- DuplicateFormat =
Class.new(StandardError)
Class Attribute Summary collapse
-
.date_format_set ⇒ Object
readonly
Get date format set for using current thread format setting.
-
.date_formats ⇒ Object
Returns the value of attribute date_formats.
-
.datetime_format_set ⇒ Object
readonly
Get datetime format set for using current thread format setting.
-
.datetime_formats ⇒ Object
Returns the value of attribute datetime_formats.
-
.format_components ⇒ Object
Returns the value of attribute format_components.
-
.format_tokens ⇒ Object
Returns the value of attribute format_tokens.
-
.time_format_set ⇒ Object
readonly
Returns the value of attribute time_format_set.
-
.time_formats ⇒ Object
Returns the value of attribute time_formats.
-
.timezone_mapping ⇒ Object
Returns the value of attribute timezone_mapping.
Class Method Summary collapse
-
.add_formats(type, *add_formats) ⇒ Object
Adds new formats.
- .compile_formats ⇒ Object
- .current_date_format ⇒ Object
- .current_date_format=(value) ⇒ Object
-
.format_sets(type, string) ⇒ Object
Returns format for type and other possible matching format set based on type and value length.
-
.remove_formats(type, *remove_formats) ⇒ Object
Delete formats of specified type.
- .sorted_token_keys ⇒ Object
-
.use_euro_formats ⇒ Object
Use date formats that return ambiguous dates parsed in European format.
-
.use_us_formats ⇒ Object
Use date formats that return ambiguous dates parsed as US format.
Class Attribute Details
.date_format_set ⇒ Object (readonly)
Get date format set for using current thread format setting
208 209 210 |
# File 'lib/timeliness/definitions.rb', line 208 def date_format_set @date_format_set end |
.date_formats ⇒ Object
Returns the value of attribute date_formats.
163 164 165 |
# File 'lib/timeliness/definitions.rb', line 163 def date_formats @date_formats end |
.datetime_format_set ⇒ Object (readonly)
Get datetime format set for using current thread format setting
214 215 216 |
# File 'lib/timeliness/definitions.rb', line 214 def datetime_format_set @datetime_format_set end |
.datetime_formats ⇒ Object
Returns the value of attribute datetime_formats.
163 164 165 |
# File 'lib/timeliness/definitions.rb', line 163 def datetime_formats @datetime_formats end |
.format_components ⇒ Object
Returns the value of attribute format_components.
163 164 165 |
# File 'lib/timeliness/definitions.rb', line 163 def format_components @format_components end |
.format_tokens ⇒ Object
Returns the value of attribute format_tokens.
163 164 165 |
# File 'lib/timeliness/definitions.rb', line 163 def format_tokens @format_tokens end |
.time_format_set ⇒ Object (readonly)
Returns the value of attribute time_format_set.
164 165 166 |
# File 'lib/timeliness/definitions.rb', line 164 def time_format_set @time_format_set end |
.time_formats ⇒ Object
Returns the value of attribute time_formats.
163 164 165 |
# File 'lib/timeliness/definitions.rb', line 163 def time_formats @time_formats end |
.timezone_mapping ⇒ Object
Returns the value of attribute timezone_mapping.
163 164 165 |
# File 'lib/timeliness/definitions.rb', line 163 def timezone_mapping @timezone_mapping end |
Class Method Details
.add_formats(type, *add_formats) ⇒ Object
Adds new formats. Must specify format type and can specify a :before option to nominate which format the new formats should be inserted in front on to take higher precedence.
Error is raised if format already exists or if :before format is not found.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/timeliness/definitions.rb', line 172 def add_formats(type, *add_formats) formats = send("#{type}_formats") = add_formats.last.is_a?(Hash) ? add_formats.pop : {} before = [:before] raise FormatNotFound, "Format for :before option #{before.inspect} was not found." if before && !formats.include?(before) add_formats.each do |format| raise DuplicateFormat, "Format #{format.inspect} is already included in #{type.inspect} formats" if formats.include?(format) index = before ? formats.index(before) : -1 formats.insert(index, format) end compile_formats end |
.compile_formats ⇒ Object
230 231 232 233 234 235 236 237 238 |
# File 'lib/timeliness/definitions.rb', line 230 def compile_formats @sorted_token_keys = nil @time_format_set = FormatSet.compile(time_formats) @us_date_format_set = FormatSet.compile(date_formats) @us_datetime_format_set = FormatSet.compile(datetime_formats) @euro_date_format_set = FormatSet.compile(date_formats.select { |format| US_FORMAT_REGEXP !~ format }) @euro_datetime_format_set = FormatSet.compile(datetime_formats.select { |format| US_FORMAT_REGEXP !~ format }) end |
.current_date_format ⇒ Object
202 203 204 |
# File 'lib/timeliness/definitions.rb', line 202 def current_date_format Thread.current["Timeliness.current_date_format"] ||= Timeliness.configuration.ambiguous_date_format end |
.current_date_format=(value) ⇒ Object
198 199 200 |
# File 'lib/timeliness/definitions.rb', line 198 def current_date_format=(value) Thread.current["Timeliness.current_date_format"] = value end |
.format_sets(type, string) ⇒ Object
Returns format for type and other possible matching format set based on type and value length. Gives minor speed-up by checking string length.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/timeliness/definitions.rb', line 247 def format_sets(type, string) case type when :date [ date_format_set, datetime_format_set ] when :datetime if string.length < 11 [ date_format_set, datetime_format_set ] else [ datetime_format_set, date_format_set ] end when :time if string.length < 11 [ time_format_set ] else [ datetime_format_set, time_format_set ] end else if string.length < 11 [ date_format_set, time_format_set, datetime_format_set ] else [ datetime_format_set, date_format_set, time_format_set ] end end end |
.remove_formats(type, *remove_formats) ⇒ Object
Delete formats of specified type. Error raised if format not found.
189 190 191 192 193 194 195 196 |
# File 'lib/timeliness/definitions.rb', line 189 def remove_formats(type, *remove_formats) remove_formats.each do |format| unless send("#{type}_formats").delete(format) raise FormatNotFound, "Format #{format.inspect} not found in #{type.inspect} formats" end end compile_formats end |
.sorted_token_keys ⇒ Object
240 241 242 |
# File 'lib/timeliness/definitions.rb', line 240 def sorted_token_keys @sorted_token_keys ||= format_tokens.keys.sort_by(&:size).reverse end |
.use_euro_formats ⇒ Object
Use date formats that return ambiguous dates parsed in European format
220 221 222 |
# File 'lib/timeliness/definitions.rb', line 220 def use_euro_formats self.current_date_format = :euro end |
.use_us_formats ⇒ Object
Use date formats that return ambiguous dates parsed as US format
226 227 228 |
# File 'lib/timeliness/definitions.rb', line 226 def use_us_formats self.current_date_format = :us end |