Sha256: 2ec4afb22aaf9212f467587bab1b7327188cb910ce3413995a3016202f635d42

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Coactive
  module Contexts
    module Inspector
      class << self
        class_attribute :max_num, :max_length, :basic_classes
        self.max_num = 3
        self.max_length = 100
        self.basic_classes = [Module, Symbol, String, Numeric, TrueClass, FalseClass, Regexp]

        def call(data)
          if data.is_a?(Array)
            inspect_array(data)
          elsif data.is_a?(Hash)
            inspect_hash(data)
          elsif basic_classes.any? { |klass| data.is_a?(klass) }
            inspect_basic(data)
          else
            inspect_object(data)
          end
        end

        private

        def inspect_array(data)
          str = data.take(max_num).map { |v| call(v) }.join(', ')
          str += "..." if data.size > max_num
          "[#{str}]"
        end

        def inspect_hash(data)
          str = data.take(max_num).map { |k, v| "#{k}: #{call(v)}" }.join(', ')
          str += "..." if data.size > max_num
          "{#{str}}"
        end

        def inspect_basic(data)
          data.inspect
        end

        def inspect_object(data)
          "#<#{data.class}:#{data.object_id}>"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coactive-0.5.0 lib/coactive/contexts/inspector.rb