Class: Logged::Formatter::KeyValue

Inherits:
Base
  • Object
show all
Defined in:
lib/logged/formatter/key_value.rb

Overview

Key-Value formatter for logged

Instance Method Summary collapse

Instance Method Details

#call(data) ⇒ Object



7
8
9
10
11
12
# File 'lib/logged/formatter/key_value.rb', line 7

def call(data)
  data
    .reject { |_k, v| v.nil? || (v.is_a?(String) && v.blank?) }
    .map { |k, v| format_key(k, v) }
    .join(' ')
end

#format_key(key, value) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/logged/formatter/key_value.rb', line 14

def format_key(key, value)
  # encapsulate in single quotes if value is a string
  value = "'#{value}'" if value.is_a?(String)

  # ensure only two decimals
  value = Kernel.format('%.2f', value) if value.is_a?(Float)

  "#{key}=#{value}"
end