Sha256: f9015beaf36f93658640968d879c48695f5f82afc01c8492de85973936fe1a4e

Contents?: true

Size: 707 Bytes

Versions: 2

Compression:

Stored size: 707 Bytes

Contents

# frozen_string_literal: true

module AddressConcern

module InspectBase
  def inspect_base(*_items, class: true, id: true)
    items = _items.map { |item|
      if item.is_a?(Hash)
        item.map { |k, v|
          "#{k}: #{v}"
        }.join(', ')
      elsif item.respond_to?(:to_proc) && item.to_proc.arity <= 0
        item.to_proc.(self)
      else
        item.to_s
      end
    }

    _class = binding.local_variable_get(:class)
    _id    = binding.local_variable_get(:id)

    '<' +
    [
      (_class == true ? self.class : _class),
      ("#{self.id || 'new'}:" if _id),
    ].join(' ') + ' ' +
    [
      *items,
    ].filter_map(&:presence).map(&:to_s).join(', ') +
    '>'
  end
end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
address_concern-3.0.0 lib/address_concern/inspect_base.rb
address_concern-2.1.1 lib/address_concern/inspect_base.rb