Sha256: d875f2ef9a3cedee810376c3141fc497024d8255f0d430d1a56eee396cab954a

Contents?: true

Size: 1.81 KB

Versions: 5

Compression:

Stored size: 1.81 KB

Contents

module VCR
  class Cassette
    # Keeps track of the cassette serializers in a hash-like object.
    class Serializers
      autoload :YAML,  'vcr/cassette/serializers/yaml'
      autoload :Syck,  'vcr/cassette/serializers/syck'
      autoload :Psych, 'vcr/cassette/serializers/psych'
      autoload :JSON,  'vcr/cassette/serializers/json'

      # @private
      def initialize
        @serializers = {}
      end

      # Gets the named serializer.
      #
      # @param name [Symbol] the name of the serializer
      # @return the named serializer
      # @raise [ArgumentError] if there is not a serializer for the given name
      def [](name)
        @serializers.fetch(name) do |_|
          @serializers[name] = case name
            when :yaml  then YAML
            when :syck  then Syck
            when :psych then Psych
            when :json  then JSON
            else raise ArgumentError.new("The requested VCR cassette serializer (#{name.inspect}) is not registered.")
          end
        end
      end

      # Registers a serializer.
      #
      # @param name [Symbol] the name of the serializer
      # @param value [#file_extension, #serialize, #deserialize] the serializer object. It must implement
      #  +file_extension()+, +serialize(Hash)+ and +deserialize(String)+.
      def []=(name, value)
        if @serializers.has_key?(name)
          warn "WARNING: There is already a VCR cassette serializer registered for #{name.inspect}. Overriding it."
        end

        @serializers[name] = value
      end
    end

    # @private
    module EncodingErrorHandling
      def handle_encoding_errors
        yield
      rescue *self::ENCODING_ERRORS => e
        e.message << "\nNote: Using VCR's `:preserve_exact_body_bytes` option may help prevent this error in the future."
        raise
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vcr-2.1.1 lib/vcr/cassette/serializers.rb
vcr-2.1.0 lib/vcr/cassette/serializers.rb
vcr-2.0.1 lib/vcr/cassette/serializers.rb
vcr-2.0.0 lib/vcr/cassette/serializers.rb
vcr-2.0.0.rc2 lib/vcr/cassette/serializers.rb