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

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

Instance Method Summary collapse

Instance Method Details

#any_instance?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/transpec/syntax/mixin/any_instance.rb', line 27

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

#any_instance_target_class_sourceObject



35
36
37
38
39
40
41
42
43
# File 'lib/transpec/syntax/mixin/any_instance.rb', line 35

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/transpec/syntax/mixin/any_instance.rb', line 45

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

#register_request_of_any_instance_inspection(rewriter) ⇒ Object



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

def register_request_of_any_instance_inspection(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