Module: Transpec::Syntax::Mixin::AnyInstance

Includes:
AST::Sexp
Included in:
Transpec::Syntax::MethodStub, ShouldReceive
Defined in:
lib/transpec/syntax/mixin/any_instance.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(syntax) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/transpec/syntax/mixin/any_instance.rb', line 11

def self.included(syntax)
  syntax.add_dynamic_analysis_request do |rewriter|
    key = :any_instance_target_class_name
    code = <<-END.gsub(/^\s+\|/, '').chomp
      |if self.class.name == 'RSpec::Mocks::AnyInstance::Recorder'
      |  if respond_to?(:klass)
      |    klass.name
      |  elsif instance_variable_defined?(:@klass)
      |    instance_variable_get(:@klass).name
      |  else
      |    nil
      |  end
      |else
      |  nil
      |end
    END
    rewriter.register_request(subject_node, key, code)
  end
end

Instance Method Details

#any_instance?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/transpec/syntax/mixin/any_instance.rb', line 31

def any_instance?
  return true unless any_instance_target_node.nil?
  node_data = runtime_node_data(subject_node)
  return false unless node_data && node_data[:any_instance_target_class_name]
  !node_data[:any_instance_target_class_name].result.nil?
end

#any_instance_target_class_sourceObject



38
39
40
41
42
43
44
45
46
# File 'lib/transpec/syntax/mixin/any_instance.rb', line 38

def any_instance_target_class_source
  return nil unless any_instance?

  if any_instance_target_node
    any_instance_target_node.loc.expression.source
  else
    runtime_node_data(subject_node)[:any_instance_target_class_name].result
  end
end

#any_instance_target_nodeObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/transpec/syntax/mixin/any_instance.rb', line 48

def any_instance_target_node
  return nil unless subject_node.type == :send
  return nil unless subject_node.children.count == 2
  receiver_node, method_name = *subject_node
  return nil unless receiver_node
  return nil unless method_name == :any_instance

  if receiver_node.type == :const || receiver_node == s(:send, nil, :described_class)
    receiver_node
  else
    nil
  end
end