Class: ObjectIdentifier::Identifier

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

Overview

ObjectIdentifier::Identifier manages construction of the inspect String.

Defined Under Namespace

Classes: ArrayWrap

Constant Summary collapse

NO_OBJECTS_INDICATOR =
"[no objects]"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects, *args, limit: nil, klass: :not_given) ⇒ Identifier

Returns a new instance of Identifier.



46
47
48
49
50
51
# File 'lib/object_identifier/identifier.rb', line 46

def initialize(objects, *args, limit: nil, klass: :not_given)
  @objects = ArrayWrap.call(objects)
  @attributes = args.empty? ? [:id] : args
  @limit = (limit || @objects.size).to_i
  @klass = klass
end

Class Method Details

.self.identify(obj, *args) ⇒ String .self.identify(obj, *args, options) ⇒ String

Class method for constructing a self-identifying string for any given object or collection of objects.

Examples:

ObjectIdentifier::Identifier.identify(
  OpenStruct.new(a: 1, b: '2', c: :"3"), :a, :b, :c)
# => "OpenStruct[a:1, b:\"2\", c::\"3\"]"

ObjectIdentifier::Identifier.identify(1, :to_s)
# => "Integer[to_s:\"1\"]"
ObjectIdentifier::Identifier.identify(nil)
# => "[no objects]"

ObjectIdentifier::Identifier.identify(%w(1 2), :to_i, :to_f)
# => "String[to_i:1, to_f:1.0], String[to_i:2, to_f:2.0]"

ObjectIdentifier::Identifier.identify((1..10).to_a, :to_f, limit: 2)
# => "Integer[to_f:1.0], Integer[to_f:2.0], ... (8 more)"

Overloads:

  • .self.identify(obj, *args) ⇒ String

    Parameters:

    • obj (Object)

      the object to identify

    • args (*)

      (optional) a list of arguments to identify for this object or for each object in this collection

  • .self.identify(obj, *args, options) ⇒ String

    Parameters:

    • obj (Object)

      the object to identify

    • args (*)

      (optional) (default :id) a list of arguments to identify for this object

    • options (Hash)

      the options for building a customized self-identifier

    Options Hash (options):

    • :klass (String, nil)

      object class name override

    • :limit (Integer)

      maximum number of objects to display from a collection

Returns:

  • (String)

    a self-identifying string like ‘Class[id:1, name:’temp’]‘



42
43
44
# File 'lib/object_identifier/identifier.rb', line 42

def self.identify(*args, **kargs)
  new(*args, **kargs).to_s
end

Instance Method Details

#to_sString

Output the self-identifying string for an instance of ObjectIdentifier::Identifier. 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 representing the object or list of objects



59
60
61
62
63
64
65
# File 'lib/object_identifier/identifier.rb', line 59

def to_s
  if many?
    format_multiple_objects
  else
    format_single_object
  end
end