Class: Lumberjack::Formatter::DateTimeFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lumberjack/formatter/date_time_formatter.rb

Overview

Format a Date, Time, or DateTime object. If you don’t specify a format in the constructor, it will use the ISO-8601 format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = nil) ⇒ DateTimeFormatter

Returns a new instance of DateTimeFormatter.

Parameters:

  • format (String) (defaults to: nil)

    The format to use when formatting the date/time object.



11
12
13
# File 'lib/lumberjack/formatter/date_time_formatter.rb', line 11

def initialize(format = nil)
  @format = format.dup.to_s.freeze unless format.nil?
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/lumberjack/formatter/date_time_formatter.rb', line 8

def format
  @format
end

Instance Method Details

#call(obj) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/lumberjack/formatter/date_time_formatter.rb', line 15

def call(obj)
  if @format && obj.respond_to?(:strftime)
    obj.strftime(@format)
  elsif obj.respond_to?(:iso8601)
    obj.iso8601
  else
    obj.to_s
  end
end