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

Class Method Summary collapse

Class Attribute Details

.date_format_setObject (readonly)

Get date format set for using current thread format setting



205
206
207
# File 'lib/timeliness/definitions.rb', line 205

def date_format_set
  @date_format_set
end

.date_formatsObject

Returns the value of attribute date_formats.



160
161
162
# File 'lib/timeliness/definitions.rb', line 160

def date_formats
  @date_formats
end

.datetime_format_setObject (readonly)

Get datetime format set for using current thread format setting



211
212
213
# File 'lib/timeliness/definitions.rb', line 211

def datetime_format_set
  @datetime_format_set
end

.datetime_formatsObject

Returns the value of attribute datetime_formats.



160
161
162
# File 'lib/timeliness/definitions.rb', line 160

def datetime_formats
  @datetime_formats
end

.format_componentsObject

Returns the value of attribute format_components.



160
161
162
# File 'lib/timeliness/definitions.rb', line 160

def format_components
  @format_components
end

.format_tokensObject

Returns the value of attribute format_tokens.



160
161
162
# File 'lib/timeliness/definitions.rb', line 160

def format_tokens
  @format_tokens
end

.time_format_setObject (readonly)

Returns the value of attribute time_format_set.



161
162
163
# File 'lib/timeliness/definitions.rb', line 161

def time_format_set
  @time_format_set
end

.time_formatsObject

Returns the value of attribute time_formats.



160
161
162
# File 'lib/timeliness/definitions.rb', line 160

def time_formats
  @time_formats
end

.timezone_mappingObject

Returns the value of attribute timezone_mapping.



160
161
162
# File 'lib/timeliness/definitions.rb', line 160

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.

Raises:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/timeliness/definitions.rb', line 169

def add_formats(type, *add_formats)
  formats = send("#{type}_formats")
  options = add_formats.last.is_a?(Hash) ? add_formats.pop : {}
  before  = options[: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_formatsObject



227
228
229
230
231
232
233
234
235
# File 'lib/timeliness/definitions.rb', line 227

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_formatObject



199
200
201
# File 'lib/timeliness/definitions.rb', line 199

def current_date_format
  Thread.current["Timeliness.current_date_format"] ||= Timeliness.configuration.ambiguous_date_format
end

.current_date_format=(value) ⇒ Object



195
196
197
# File 'lib/timeliness/definitions.rb', line 195

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.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/timeliness/definitions.rb', line 244

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.



186
187
188
189
190
191
192
193
# File 'lib/timeliness/definitions.rb', line 186

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_keysObject



237
238
239
# File 'lib/timeliness/definitions.rb', line 237

def sorted_token_keys
  @sorted_token_keys ||= format_tokens.keys.sort_by(&:size).reverse
end

.use_euro_formatsObject

Use date formats that return ambiguous dates parsed in European format



217
218
219
# File 'lib/timeliness/definitions.rb', line 217

def use_euro_formats
  self.current_date_format = :euro
end

.use_us_formatsObject

Use date formats that return ambiguous dates parsed as US format



223
224
225
# File 'lib/timeliness/definitions.rb', line 223

def use_us_formats
  self.current_date_format = :us
end