Class: Lumberjack::Formatter::IdFormatter

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

Overview

Format an object that has an id as a hash with keys for class and id. This formatter is useful as a default formatter for objects pulled from a data store. By default it will use :id as the id attribute.

Instance Method Summary collapse

Constructor Details

#initialize(id_attribute = :id) ⇒ IdFormatter

Returns a new instance of IdFormatter.

Parameters:

  • id_attribute (Symbol, String) (defaults to: :id)

    The attribute to use as the id.



10
11
12
# File 'lib/lumberjack/formatter/id_formatter.rb', line 10

def initialize(id_attribute = :id)
  @id_attribute = id_attribute
end

Instance Method Details

#call(obj) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/lumberjack/formatter/id_formatter.rb', line 14

def call(obj)
  if obj.respond_to?(@id_attribute)
    id = obj.send(@id_attribute)
    {"class" => obj.class.name, "id" => id}
  else
    obj.to_s
  end
end