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



203
204
205
# File 'lib/timeliness/definitions.rb', line 203

def date_format_set
  @date_format_set
end

.date_formatsObject

Returns the value of attribute date_formats.



158
159
160
# File 'lib/timeliness/definitions.rb', line 158

def date_formats
  @date_formats
end

.datetime_format_setObject (readonly)

Get datetime format set for using current thread format setting



209
210
211
# File 'lib/timeliness/definitions.rb', line 209

def datetime_format_set
  @datetime_format_set
end

.datetime_formatsObject

Returns the value of attribute datetime_formats.



158
159
160
# File 'lib/timeliness/definitions.rb', line 158

def datetime_formats
  @datetime_formats
end

.format_componentsObject

Returns the value of attribute format_components.



158
159
160
# File 'lib/timeliness/definitions.rb', line 158

def format_components
  @format_components
end

.format_tokensObject

Returns the value of attribute format_tokens.



158
159
160
# File 'lib/timeliness/definitions.rb', line 158

def format_tokens
  @format_tokens
end

.time_format_setObject (readonly)

Returns the value of attribute time_format_set.



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

def time_format_set
  @time_format_set
end

.time_formatsObject

Returns the value of attribute time_formats.



158
159
160
# File 'lib/timeliness/definitions.rb', line 158

def time_formats
  @time_formats
end

.timezone_mappingObject

Returns the value of attribute timezone_mapping.



158
159
160
# File 'lib/timeliness/definitions.rb', line 158

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:



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 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_formatsObject



225
226
227
228
229
230
231
232
233
# 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_formatObject



197
198
199
# 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) ⇒ Object



193
194
195
# File 'lib/timeliness/definitions.rb', line 193

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.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# 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) ⇒ Object

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



184
185
186
187
188
189
190
191
# 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_keysObject



235
236
237
# File 'lib/timeliness/definitions.rb', line 235

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



215
216
217
# File 'lib/timeliness/definitions.rb', line 215

def use_euro_formats
  self.current_date_format = :euro
end

.use_us_formatsObject

Use date formats that return ambiguous dates parsed as US format



221
222
223
# File 'lib/timeliness/definitions.rb', line 221

def use_us_formats
  self.current_date_format = :us
end