Module: RSpec::Support::ObjectFormatter
- Defined in:
- lib/rspec/support/object_formatter.rb
Overview
Provide additional output details beyond what ‘inspect` provides when printing Time, DateTime, or BigDecimal
Defined Under Namespace
Classes: InspectableItem
Constant Summary collapse
- TIME_FORMAT =
- "%Y-%m-%d %H:%M:%S"
- DATE_TIME_FORMAT =
- "%a, %d %b %Y %H:%M:%S.%N %z"
Class Method Summary collapse
- .format(object) ⇒ Object private
- 
  
    
      .format_date_time(date_time)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    ActiveSupport sometimes overrides inspect. 
- .format_time(time) ⇒ Object
- 
  
    
      .prepare_for_inspection(object)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    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. 
- .prepare_hash(input) ⇒ Object
Class Method Details
.format(object) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
| 7 8 9 | # File 'lib/rspec/support/object_formatter.rb', line 7 def self.format(object) prepare_for_inspection(object).inspect end | 
.format_date_time(date_time) ⇒ Object
ActiveSupport sometimes overrides inspect. If ‘ActiveSupport` is defined use a custom format string that includes more time precision.
| 73 74 75 76 77 78 79 | # 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) ⇒ Object
| 59 60 61 | # 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) ⇒ Object
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.
| 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | # 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) ⇒ Object
| 48 49 50 51 52 53 | # 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 |