Class: Sapience::Formatters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sapience/formatters/base.rb

Direct Known Subclasses

Color, Default

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Parameters

time_format: [String|Symbol|nil]
  See Time#strftime for the format of this string
  :iso_8601 Outputs an ISO8601 Formatted timestamp
  nil:      Returns Empty string for time ( no time is output ).
  Default: '%Y-%m-%d %H:%M:%S.%6N'


13
14
15
16
17
18
19
20
21
# File 'lib/sapience/formatters/base.rb', line 13

def initialize(options = {})
  options          = options.dup
  @precision       = 6
  default_format   = "%Y-%m-%d %H:%M:%S.%#{precision}N"
  @time_format     = options.key?(:time_format) ? options.delete(:time_format) : default_format
  @log_host        = options.key?(:log_host) ? options.delete(:log_host) : true
  @log_application = options.key?(:log_application) ? options.delete(:log_application) : true
  fail(ArgumentError, "Unknown options: #{options.inspect}") unless options.empty?
end

Instance Attribute Details

#log_applicationObject

Returns the value of attribute log_application.



5
6
7
# File 'lib/sapience/formatters/base.rb', line 5

def log_application
  @log_application
end

#log_hostObject

Returns the value of attribute log_host.



5
6
7
# File 'lib/sapience/formatters/base.rb', line 5

def log_host
  @log_host
end

#precisionObject

Returns the value of attribute precision.



5
6
7
# File 'lib/sapience/formatters/base.rb', line 5

def precision
  @precision
end

#time_formatObject

Returns the value of attribute time_format.



5
6
7
# File 'lib/sapience/formatters/base.rb', line 5

def time_format
  @time_format
end

Instance Method Details

#format_time(time) ⇒ Object

Return the Time as a formatted string



24
25
26
27
28
29
30
31
32
33
# File 'lib/sapience/formatters/base.rb', line 24

def format_time(time)
  case time_format
  when :iso_8601
    time.utc.iso8601(precision)
  when nil
    ""
  else
    time.strftime(time_format)
  end
end