Class: ObjectIdentifier::StringFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/object_identifier/formatters/string_formatter.rb

Overview

ObjectIdentifier::StringFormatter builds a String to identify the given object(s).

Constant Summary collapse

NO_OBJECTS_INDICATOR =
"[no objects]"
KLASS_NOT_GIVEN =
"NOT_GIVEN"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects, attributes = ObjectIdentifier::Identifier.default_attributes, limit: nil, klass: KLASS_NOT_GIVEN) ⇒ StringFormatter

Returns a new instance of StringFormatter.

Parameters:

  • objects (Object, [Object, ...])

    the object(s) to be interrogated for String values to be added to the output String

  • attributes (Array, *args) (defaults to: ObjectIdentifier::Identifier.default_attributes)

    a list of method calls to interrogate the given object(s) with

  • limit (Integer, nil) (defaults to: nil)

    a given limit on the number of objects to interrogate

  • klass (String, Symbol) (defaults to: KLASS_NOT_GIVEN)

    a preferred type name for identifying the given object(s) as



21
22
23
24
25
26
27
28
29
30
# File 'lib/object_identifier/formatters/string_formatter.rb', line 21

def initialize(
      objects,
      attributes = ObjectIdentifier::Identifier.default_attributes,
      limit: nil,
      klass: KLASS_NOT_GIVEN)
  @objects = ObjectIdentifier::ArrayWrap.(objects)
  @attributes = ObjectIdentifier::ArrayWrap.(attributes)
  @limit = (limit || @objects.size).to_i
  @klass = klass.to_s
end

Class Method Details

.call(objects, *attributes, **kargs) ⇒ Object



9
10
11
# File 'lib/object_identifier/formatters/string_formatter.rb', line 9

def self.call(objects, *attributes, **kargs)
  new(objects, *attributes, **kargs).call
end

Instance Method Details

#callString

Output the self-identifying string for the given object(s). Will either return a single object representation or a list of object representations, based on the number of objects we’re identifying.

Returns:

  • (String)

    a string that identifies the object(s)



37
38
39
40
41
42
43
# File 'lib/object_identifier/formatters/string_formatter.rb', line 37

def call
  return NO_OBJECTS_INDICATOR if @objects.empty?

  output_strings = @objects.first(@limit).map { |obj| format(obj) }
  output_strings << "... (#{truncated_objects_count} more)" if truncated?
  output_strings.join(", ")
end