Sha256: 549bfc9513f700c8975f28fb0dac1835c1cca662d7faa4212f1384f95bc89f24

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

module VCR
  class Cassette
    # @private
    class Reader
      def initialize(file_name, erb)
        @file_name, @erb = file_name, erb
      end

      def read
        return file_content unless use_erb?
        binding = binding_for_variables if erb_variables
        template.result(binding)
      rescue NameError => e
        handle_name_error(e)
      end

    private

      def handle_name_error(e)
        example_hash = (erb_variables || {}).merge(e.name => 'some value')

        raise Errors::MissingERBVariableError.new(
          "The ERB in the #{@file_name} cassette file references undefined variable #{e.name}.  " +
          "Pass it to the cassette using :erb => #{ example_hash.inspect }."
        )
      end

      def use_erb?
        !!@erb
      end

      def erb_variables
        @erb if @erb.is_a?(Hash)
      end

      def file_content
        @file_content ||= File.read(@file_name)
      end

      def template
        @template ||= ERB.new(file_content)
      end

      @@struct_cache = Hash.new do |hash, attributes|
        hash[attributes] = Struct.new(*attributes)
      end

      def variables_object
        @variables_object ||= @@struct_cache[erb_variables.keys].new(*erb_variables.values)
      end

      def binding_for_variables
        @binding_for_variables ||= variables_object.instance_eval { binding }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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