Class: Transpec::Syntax::Have::DynamicInspector
- Inherits:
-
Object
- Object
- Transpec::Syntax::Have::DynamicInspector
- Defined in:
- lib/transpec/syntax/have.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#available_query_methods_inspection_code ⇒ Object
rubocop:enable MethodLength.
-
#collection_accessor_inspection_code ⇒ Object
rubocop:disable MethodLength.
-
#initialize(have, rewriter) ⇒ DynamicInspector
constructor
A new instance of DynamicInspector.
- #register_request ⇒ Object
- #subject_code ⇒ Object
- #target_node ⇒ Object
- #target_type ⇒ Object
Constructor Details
#initialize(have, rewriter) ⇒ DynamicInspector
Returns a new instance of DynamicInspector.
187 188 189 190 |
# File 'lib/transpec/syntax/have.rb', line 187 def initialize(have, rewriter) @have = have @rewriter = rewriter end |
Class Method Details
.register_request(have, rewriter) ⇒ Object
183 184 185 |
# File 'lib/transpec/syntax/have.rb', line 183 def self.register_request(have, rewriter) new(have, rewriter).register_request end |
Instance Method Details
#available_query_methods_inspection_code ⇒ Object
rubocop:enable MethodLength
277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/transpec/syntax/have.rb', line 277 def available_query_methods_inspection_code " |collection_accessor = \#{collection_accessor_inspection_code}\n |target = if collection_accessor\n | \#{subject_code}.__send__(collection_accessor)\n | else\n | \#{subject_code}\n | end\n |target.methods & \#{QUERY_METHOD_PRIORITIES.inspect}\n END\nend\n".gsub(/^\s+\|/, '').chomp |
#collection_accessor_inspection_code ⇒ Object
rubocop:disable MethodLength
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/transpec/syntax/have.rb', line 235 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. # # rubocop:disable LineLength # https://github.com/rspec/rspec-expectations/blob/v2.14.3/lib/rspec/matchers/built_in/have.rb#L48-L58 # rubocop:enable LineLength @collection_accessor_inspection_code ||= " |begin\n | exact_name = \#{@have.items_name.inspect}\n |\n | inflector = if defined?(ActiveSupport::Inflector) &&\n | ActiveSupport::Inflector.respond_to?(:pluralize)\n | ActiveSupport::Inflector\n | elsif defined?(Inflector)\n | Inflector\n | else\n | nil\n | end\n |\n | if inflector\n | pluralized_name = inflector.pluralize(exact_name).to_sym\n | respond_to_pluralized_name = \#{subject_code}.respond_to?(pluralized_name)\n | end\n |\n | respond_to_query_methods =\n | !(\#{subject_code}.methods & \#{QUERY_METHOD_PRIORITIES.inspect}).empty?\n |\n | if \#{subject_code}.respond_to?(exact_name)\n | exact_name\n | elsif respond_to_pluralized_name\n | pluralized_name\n | elsif respond_to_query_methods\n | nil\n | else\n | exact_name\n | end\n |end\n END\nend\n".gsub(/^\s+\|/, '').chomp |
#register_request ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/transpec/syntax/have.rb', line 208 def register_request key = :collection_accessor code = collection_accessor_inspection_code @rewriter.register_request(target_node, key, code, target_type) # Give up inspecting query methods of collection accessor with arguments # (e.g. have(2).errors_on(variable)) since this is a context of #instance_eval. unless @have.items_method_has_arguments? key = :available_query_methods code = available_query_methods_inspection_code @rewriter.register_request(target_node, key, code, target_type) end key = :collection_accessor_is_private? code = "#{subject_code}.private_methods.include?(#{@have.items_name.inspect})" @rewriter.register_request(target_node, key, code, target_type) key = :project_requires_collection_matcher? code = 'defined?(RSpec::Rails) || defined?(RSpec::CollectionMatchers)' @rewriter.register_request(target_node, key, code, :context) end |
#subject_code ⇒ Object
230 231 232 |
# File 'lib/transpec/syntax/have.rb', line 230 def subject_code @have.explicit_subject? ? 'self' : 'subject' end |
#target_node ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/transpec/syntax/have.rb', line 192 def target_node if @have.explicit_subject? @have.expectation.subject_node else @have.expectation.node end end |
#target_type ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/transpec/syntax/have.rb', line 200 def target_type if @have.explicit_subject? :object else :context end end |