Class: VCR::UnusedCassettes::CallContext

Inherits:
Object
  • Object
show all
Defined in:
lib/vcr/unused_cassettes/call_context.rb

Constant Summary collapse

ValueUnresolveable =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initializeCallContext



7
8
9
10
11
12
# File 'lib/vcr/unused_cassettes/call_context.rb', line 7

def initialize
  @context = {
    variables: {},
    constants: {}
  }
end

Instance Method Details

#extract_value(node, string_interpolation_error: :raise) ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vcr/unused_cassettes/call_context.rb', line 34

def extract_value(node, string_interpolation_error: :raise)
  case node.type
  when :nil_node
    nil
  when :string_node
    node.unescaped
  when :symbol_node
    node.unescaped.to_sym
  when :hash_node, :keyword_hash_node
    node.elements.each_with_object({}) do |element, hash|
      if element.type == :assoc_splat_node
        hash.merge(extract_value(element.value, string_interpolation_error: string_interpolation_error))
        next
      end
      key = extract_value(element.key, string_interpolation_error: string_interpolation_error)
      value = extract_value(element.value, string_interpolation_error: string_interpolation_error)
      hash[key] = value
    rescue ValueUnresolveable
      next
    end
  when :array_node
    node.elements.map do |element|
      extract_value(element, string_interpolation_error: string_interpolation_error)
    end
  when :interpolated_string_node
    node.parts.map do |part_node|
      if part_node.type == :embedded_statements_node
        if part_node.statements.body.size != 1
          if string_interpolation_error == :raise
            raise ValueUnresolveable, "Could not resolve value for node: #{part_node.inspect}"
          elsif string_interpolation_error == :wildcard
            "*"
          end
        else
          extract_value(part_node.statements.body.first, string_interpolation_error: string_interpolation_error)
        end
      else
        extract_value(part_node, string_interpolation_error: string_interpolation_error)
      end
    rescue ValueUnresolveable
      if string_interpolation_error == :raise
        raise ValueUnresolveable, "Could not resolve value for node: #{part_node.inspect}"
      elsif string_interpolation_error == :wildcard
        "*"
      end
    end.join
  when :local_variable_read_node, :instance_variable_read_node
    resolve_variable(node.name)
  when :constant_read_node
    @context.dig(:constants, node.name)
  when :assoc_splat_node
    extract_value(node.value, string_interpolation_error: string_interpolation_error)
  else
    if node.respond_to?(:value) && !node.value.is_a?(Prism::Node)
      node.value
    else
      raise ValueUnresolveable, "Could not resolve value for node: #{node.inspect}"
    end
  end
end

#resolve_constant(constant_name) ⇒ Object



30
31
32
# File 'lib/vcr/unused_cassettes/call_context.rb', line 30

def resolve_constant(constant_name)
  @context.dig(:constants, constant_name)
end

#resolve_variable(variable_name) ⇒ Object



26
27
28
# File 'lib/vcr/unused_cassettes/call_context.rb', line 26

def resolve_variable(variable_name)
  @context.dig(:variables, variable_name)
end

#track(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vcr/unused_cassettes/call_context.rb', line 14

def track(node)
  reset_local_variables! if new_test_node?(node)
  case node.type
  when :local_variable_write_node
    store_variable(node.name, node.value)
  when :constant_write_node
    store_constant(node.name, node.value)
  when :instance_variable_write_node
    store_variable(node.name, node.value)
  end
end