Class: TCR::Cassette

Inherits:
Object
  • Object
show all
Defined in:
lib/tcr/cassette.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Cassette

Returns a new instance of Cassette.



9
10
11
12
13
14
15
16
17
18
# File 'lib/tcr/cassette.rb', line 9

def initialize(name)
  @name = name

  if recording?
    verify_cassette_path_is_writable
    @sessions = []
  else
    @sessions = unmarshal(File.read(filename))
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/tcr/cassette.rb', line 7

def name
  @name
end

Instance Method Details

#check_hits_all_sessionsObject

Raises:



43
44
45
# File 'lib/tcr/cassette.rb', line 43

def check_hits_all_sessions
  raise ExtraSessionsError if !recording? && !@sessions.empty?
end

#next_sessionObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/tcr/cassette.rb', line 24

def next_session
  if recording?
    @sessions << []
    @sessions.last
  else
    raise NoMoreSessionsError if @sessions.empty?

    @sessions.shift
  end
end

#recording?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/tcr/cassette.rb', line 20

def recording?
  @recording ||= !File.exist?(filename)
end

#saveObject



35
36
37
38
39
40
41
# File 'lib/tcr/cassette.rb', line 35

def save
  return unless recording?

  File.write(filename, marshal(@sessions))
rescue Encoding::UndefinedConversionError
  File.binwrite(filename, marshal(@sessions))
end