Class: SeeingIsBelieving::Line

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HasException
Defined in:
lib/seeing_is_believing/line.rb

Overview

thin wrapper over an array, used by the result

Instance Attribute Summary

Attributes included from HasException

#exception

Instance Method Summary collapse

Constructor Details

#initialize(array = [], max_number_of_captures = Float::INFINITY) ⇒ Line

Returns a new instance of Line.



22
23
24
25
26
27
28
# File 'lib/seeing_is_believing/line.rb', line 22

def initialize(array = [], max_number_of_captures=Float::INFINITY)
  @array                  = []
  @max_number_of_captures = max_number_of_captures
  @num_results            = 0
  @total_size             = 0
  array.each { |element| record_result element }
end

Instance Method Details

#==(ary_or_line) ⇒ Object



40
41
42
43
# File 'lib/seeing_is_believing/line.rb', line 40

def ==(ary_or_line)
  return @array == ary_or_line if Array === ary_or_line
  ary_or_line == @array && exception == ary_or_line.exception
end

#inspectObject



45
46
47
48
# File 'lib/seeing_is_believing/line.rb', line 45

def inspect
  inspected_exception = has_exception? ? "#{exception.class}:#{exception.message.inspect}" : "no exception"
  "#<SIB:Line#{@array.inspect} (#@num_results, #@total_size) #{inspected_exception}>"
end

#record_result(value) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/seeing_is_believing/line.rb', line 30

def record_result(value)
  inspected = value.inspect # only invoke inspect once, b/c the inspection may be recorded
  if    size <  @max_number_of_captures then @array << inspected
  elsif size == @max_number_of_captures then @array << '...'
  end
  @num_results += 1
  @total_size  += inspected.size
  self
end

#to_aObject Also known as: to_ary



17
18
19
# File 'lib/seeing_is_believing/line.rb', line 17

def to_a
  @array.dup
end