Module: RSpec::Support::ObjectFormatter

### Home Classes Methods

### In Files

- lib/rspec/support/object_formatter.rb

### Methods

- ::format
- ::format_date_time
- ::format_time
- ::prepare_for_inspection
- ::prepare_hash

### Class/Module Index

Quicksearch

- RSpec
- RSpec::CallerFilter
- RSpec::Support
- RSpec::Support::BlockSignature
- RSpec::Support::Differ
- RSpec::Support::DirectoryMaker
- RSpec::Support::EncodedString
- RSpec::Support::FormattingSupport
- RSpec::Support::FuzzyMatcher
- RSpec::Support::HunkGenerator
- RSpec::Support::InSubProcess
- RSpec::Support::LooseSignatureVerifier
- RSpec::Support::LooseSignatureVerifier::SignatureWithKeywordArgumentsMatcher
- RSpec::Support::MethodSignature
- RSpec::Support::MethodSignatureVerifier
- RSpec::Support::OS
- RSpec::Support::ObjectFormatter
- RSpec::Support::RecursiveConstMethods
- RSpec::Support::Ruby
- RSpec::Support::RubyFeatures
- RSpec::Support::ShellOut
- RSpec::Support::Spec
- RSpec::Support::StdErrSplitter
- RSpec::Support::StrictSignatureVerifier
- RSpec::Support::Version
- RSpec::Support::VersionChecker
- RSpec::Support::Warnings
- RSpec::Support::WithIsolatedStdErr
- Object
- RSpecHelpers

No matching classes.

# RSpec::Support::ObjectFormatter

Provide additional output details beyond what `inspect` provides when printing Time, DateTime, or BigDecimal

### Constants

DATE_TIME_FORMAT
InspectableItem

  @private

TIME_FORMAT

### Public Class Methods

format(object) click to toggle source

@api private

    # File lib/rspec/support/object_formatter.rb, line 7
    def self.format(object)
      prepare_for_inspection(object).inspect
    end

format_date_time(date_time) click to toggle source

ActiveSupport sometimes overrides inspect. If `ActiveSupport` is defined use a custom format string that includes more time precision. @private

    # File lib/rspec/support/object_formatter.rb, line 73
    def self.format_date_time(date_time)
      if defined?(ActiveSupport)
        date_time.strftime(DATE_TIME_FORMAT)
      else
        date_time.inspect
      end
    end

format_time(time) click to toggle source

@private

    # File lib/rspec/support/object_formatter.rb, line 59
    def self.format_time(time)
      time.strftime("#{TIME_FORMAT}.#{"%09d" % time.nsec} %z")
    end

prepare_for_inspection(object) click to toggle source

@private Prepares the provided object to be formatted by wrapping it as needed in something that, when `inspect` is called on it, will produce the desired output.

This allows us to apply the desired formatting to hash/array data structures at any level of nesting, simply by walking that structure and replacing items with custom items that have `inspect` defined to return the desired output for that item. Then we can just use `Array#inspect` or `Hash#inspect` to format the entire thing.

    # File lib/rspec/support/object_formatter.rb, line 23
    def self.prepare_for_inspection(object)
      case object
      when Array
        return object.map { |o| prepare_for_inspection(o) }
      when Hash
        return prepare_hash(object)
      when Time
        inspection = format_time(object)
      else
        if defined?(DateTime) && DateTime === object
          inspection = format_date_time(object)
        elsif defined?(BigDecimal) && BigDecimal === object
          inspection = "#{object.to_s 'F'} (#{object.inspect})"
        elsif RSpec::Support.is_a_matcher?(object) && object.respond_to?(:description)
          inspection = object.description
        else
          return object
        end
      end

      InspectableItem.new(inspection)
    end

prepare_hash(input) click to toggle source

@private

    # File lib/rspec/support/object_formatter.rb, line 48
    def self.prepare_hash(input)
      input.inject({}) do |hash, (k, v)|
        hash[prepare_for_inspection(k)] = prepare_for_inspection(v)
        hash
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.