module Timeliness::Definitions

Constants

DuplicateFormat
FormatNotFound
US_FORMAT_REGEXP

Attributes

date_format_set[R]
date_formats[RW]
datetime_format_set[R]
datetime_formats[RW]
format_components[RW]
format_tokens[RW]
time_format_set[R]
time_formats[RW]
timezone_mapping[RW]

Public Class Methods

add_formats(type, *add_formats) click to toggle source

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.

# File lib/timeliness/definitions.rb, line 167
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_formats() click to toggle source
# File lib/timeliness/definitions.rb, line 225
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() click to toggle source
# File lib/timeliness/definitions.rb, line 197
def current_date_format
  Thread.current["Timeliness.current_date_format"] ||= Timeliness.configuration.ambiguous_date_format
end
current_date_format=(value) click to toggle source
# File lib/timeliness/definitions.rb, line 193
def current_date_format=(value)
  Thread.current["Timeliness.current_date_format"] = value
end
format_sets(type, string) click to toggle source

Returns format for type and other possible matching format set based on type and value length. Gives minor speed-up by checking string length.

# File lib/timeliness/definitions.rb, line 242
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) click to toggle source

Delete formats of specified type. Error raised if format not found.

# File lib/timeliness/definitions.rb, line 184
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() click to toggle source
# File lib/timeliness/definitions.rb, line 235
def sorted_token_keys
  @sorted_token_keys ||= format_tokens.keys.sort_by(&:size).reverse
end
use_euro_formats() click to toggle source

Use date formats that return ambiguous dates parsed in European format

# File lib/timeliness/definitions.rb, line 215
def use_euro_formats
  self.current_date_format = :euro
end
use_us_formats() click to toggle source

Use date formats that return ambiguous dates parsed as US format

# File lib/timeliness/definitions.rb, line 221
def use_us_formats
  self.current_date_format = :us
end