Class: Specdiff::Inspect

Inherits:
Object
  • Object
show all
Defined in:
lib/specdiff/inspect.rb

Defined Under Namespace

Classes: InspectWrapper

Constant Summary collapse

TIME_FORMAT =
"%Y-%m-%d %H:%M:%S %z"
DATE_FORMAT =
"%Y-%m-%d"
STANDARD_INSPECT_RECURSIVE_ARRAY =

The stdlib inspect code returns this when you have recursive structures.

"[...]".freeze
STANDARD_INSPECT_RECURSIVE_HASH =
"{...}".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInspect

Returns a new instance of Inspect.



9
10
11
# File 'lib/specdiff/inspect.rb', line 9

def initialize
  @recursion_trail = []
end

Class Method Details

.callObject



5
6
7
# File 'lib/specdiff/inspect.rb', line 5

def self.call(...)
  new.call(...)
end

Instance Method Details

#call(thing) ⇒ Object

#=== allows us to rely on Module implementing #=== instead of relying on the thing (which could be any kind of wacky object) having to implement #is_a? or #kind_of?



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/specdiff/inspect.rb', line 16

def call(thing)
  if Hash === thing || Array === thing
    recursive_replace_inspect(thing).inspect
  elsif Time === thing
    "#<Time: #{thing.strftime(TIME_FORMAT)}>"
  elsif DateTime === thing
    "#<DateTime: #{thing.rfc3339}>"
  elsif Date === thing
    "#<Date: #{thing.strftime(DATE_FORMAT)}>"
  elsif defined?(BigDecimal) && BigDecimal === thing
    "#<BigDecimal: #{thing.to_s('F')}>"
  elsif rspec_matcher?(thing)
    # Turns out rspec depends on the recursion in its inspection logic to
    # print the "description" of rspec matchers, in situations such as when
    # using multi-matchers (.all, .or or .and), or when nesting them inside
    # eachother (such as match([have_attributes(...)])).
    thing.description
  else
    begin
      thing.inspect
    rescue NoMethodError
      inspect_anyway(thing)
    end
  end
end