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 163
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 203
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 })

  @date_format_set     = @us_date_format_set
  @datetime_format_set = @us_datetime_format_set
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 223
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 180
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 216
def sorted_token_keys
  @sorted_token_keys ||= format_tokens.keys.sort {|a,b| a.size <=> b.size }.reverse
end
use_euro_formats() click to toggle source

Removes US date formats so that ambiguous dates are parsed as European format

# File lib/timeliness/definitions.rb, line 191
def use_euro_formats
  @date_format_set     = @euro_date_format_set
  @datetime_format_set = @euro_datetime_format_set
end
use_us_formats() click to toggle source

Restores default to parse ambiguous dates as US format

# File lib/timeliness/definitions.rb, line 198
def use_us_formats
  @date_format_set     = @us_date_format_set
  @datetime_format_set = @us_datetime_format_set
end