File: linked_cassette.rb

package info (click to toggle)
ruby-vcr 6.0.0%2Breally5.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,320 kB
  • sloc: ruby: 8,456; sh: 177; makefile: 7
file content (72 lines) | stat: -rw-r--r-- 1,953 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'delegate'
require 'vcr/errors'

module VCR
  # A Cassette wrapper for linking cassettes from another thread
  class LinkedCassette < SimpleDelegator
    # An enumerable lazily wrapping a list of cassettes that a context is using
    class CassetteList
      include Enumerable

      # Creates a new list of context-owned cassettes and linked cassettes
      # @param [Array] context-owned cassettes
      # @param [Array] context-unowned (linked) cassettes
      def initialize(cassettes, linked_cassettes)
        @cassettes = cassettes
        @linked_cassettes = linked_cassettes
      end

      # Yields linked cassettes first, and then context-owned cassettes
      def each
        @linked_cassettes.each do |cassette|
          yield wrap(cassette)
        end

        @cassettes.each do |cassette|
          yield cassette
        end
      end

      # Provide last implementation, which is not provided by Enumerable
      def last
        cassette = @cassettes.last
        return cassette if cassette

        cassette = @linked_cassettes.last
        wrap(cassette) if cassette
      end

      # Provide size implementation, which is not provided by Enumerable
      def size
        @cassettes.size + @linked_cassettes.size
      end

    protected
      def wrap(cassette)
        if cassette.linked?
          cassette
        else
          LinkedCassette.new(cassette)
        end
      end
    end

    # Create a new CassetteList
    # @param [Array] context-owned cassettes
    # @param [Array] context-unowned (linked) cassettes
    def self.list(cassettes, linked_cassettes)
      CassetteList.new(cassettes, linked_cassettes)
    end

    # Prevents cassette ejection by raising EjectLinkedCassetteError
    def eject(*args)
      raise Errors::EjectLinkedCassetteError,
        "cannot eject a cassette inserted by a parent thread"
    end

    # @return [Boolean] true
    def linked?
      true
    end
  end
end