Module: Transpec::Syntax::Have::DynamicAnalysis

Extended by:
ActiveSupport::Concern
Defined in:
lib/transpec/syntax/have/dynamic_analysis.rb

Instance Method Summary collapse

Instance Method Details

#available_query_methods_inspection_codeObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/transpec/syntax/have/dynamic_analysis.rb', line 85

def available_query_methods_inspection_code
  <<-END.gsub(/^\s+\|/, '').chomp
    |collection_accessor = #{collection_accessor_inspection_code}
    |target = if collection_accessor
    |           #{subject_code}.__send__(collection_accessor)
    |         else
    |           #{subject_code}
    |         end
    |target.methods & #{QUERY_METHOD_PRIORITIES.inspect}
  END
end

#collection_accessor_inspection_codeObject



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
# File 'lib/transpec/syntax/have/dynamic_analysis.rb', line 46

def collection_accessor_inspection_code
  # `expect(owner).to have(n).things` invokes private owner#things with Object#__send__
  # if the owner does not respond to any of #size, #count and #length.
  #
  # https://github.com/rspec/rspec-expectations/blob/v2.14.3/lib/rspec/matchers/built_in/have.rb#L48-L58
  @collection_accessor_inspection_code ||= <<-END.gsub(/^\s+\|/, '').chomp
    |begin
    |  exact_name = #{items_name.inspect}
    |
    |  inflector = if defined?(ActiveSupport::Inflector) &&
    |                   ActiveSupport::Inflector.respond_to?(:pluralize)
    |                ActiveSupport::Inflector
    |              elsif defined?(Inflector)
    |                Inflector
    |              else
    |                nil
    |              end
    |
    |  if inflector
    |    pluralized_name = inflector.pluralize(exact_name).to_sym
    |    respond_to_pluralized_name = #{subject_code}.respond_to?(pluralized_name)
    |  end
    |
    |  respond_to_query_methods =
    |    !(#{subject_code}.methods & #{QUERY_METHOD_PRIORITIES.inspect}).empty?
    |
    |  if #{subject_code}.respond_to?(exact_name)
    |    exact_name
    |  elsif respond_to_pluralized_name
    |    pluralized_name
    |  elsif respond_to_query_methods
    |    nil
    |  else
    |    exact_name
    |  end
    |end
  END
end

#subject_codeObject



42
43
44
# File 'lib/transpec/syntax/have/dynamic_analysis.rb', line 42

def subject_code
  explicit_subject? ? 'self' : 'subject'
end