Module: RailsBestPractices::Core::Check::Callable

Defined in:
lib/rails_best_practices/core/check.rb

Overview

Helper to add callbacks to mark the methods are used.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rails_best_practices/core/check.rb', line 265

def self.included(base)
  base.class_eval do
    interesting_nodes :call, :fcall, :var_ref, :vcall, :command_call, :command, :alias, :bare_assoc_hash, :method_add_arg

    # remembe the message of call node.
    add_callback "start_call" do |node|
      mark_used(node.message)
    end

    # remembe the message of fcall node.
    add_callback "start_fcall" do |node|
      mark_used(node.message)
    end

    # remembe name of var_ref node.
    add_callback "start_var_ref" do |node|
      mark_used(node)
    end

    # remembe name of vcall node.
    add_callback "start_vcall" do |node|
      mark_used(node)
    end

    # skip start_command callback for these nodes
    def skip_command_callback_nodes
      []
    end

    # remember the message of command node.
    # remember the argument of alias_method and alias_method_chain as well.
    add_callback "start_command" do |node|
      case node.message.to_s
      when *skip_command_callback_nodes
        # nothing
      when "alias_method"
        mark_used(node.arguments.all[1])
      when "alias_method_chain"
        method, feature = *node.arguments.all.map(&:to_s)
        call_method("#{method}_with_#{feature}")
      when /^(before|after)_/
        node.arguments.all.each { |argument| mark_used(argument) }
      else
        mark_used(node.message)
        last_argument = node.arguments.all.last
        if last_argument.present? && :bare_assoc_hash == last_argument.sexp_type
          last_argument.hash_values.each { |argument_value| mark_used(argument_value) }
        end
      end
    end

    # remembe the message of command call node.
    add_callback "start_command_call" do |node|
      mark_used(node.message)
    end

    # remember the old method of alias node.
    add_callback "start_alias" do |node|
      mark_used(node.old_method)
    end

    # remember hash values for hash key "methods".
    #
    #     def to_xml(options = {})
    #       super options.merge(:exclude => :visible, :methods => [:is_discussion_conversation])
    #     end
    add_callback "start_bare_assoc_hash" do |node|
      if node.hash_keys.include? "methods"
        mark_used(node.hash_value("methods"))
      end
    end

    # remember the first argument for try and send method.
    add_callback "start_method_add_arg" do |node|
      case node.message.to_s
      when "try"
        mark_used(node.arguments.all.first)
      when "send"
        if [:symbol_literal, :string_literal].include?(node.arguments.all.first.sexp_type)
          mark_used(node.arguments.all.first)
        end
      else
        # nothing
      end
    end

    private
      def mark_used(method_node)
        if :bare_assoc_hash == method_node.sexp_type
          method_node.hash_values.each { |value_node| mark_used(value_node) }
        elsif :array == method_node.sexp_type
          method_node.array_values.each { |value_node| mark_used(value_node) }
        else
          method_name = method_node.to_s
        end
        call_method(method_name)
      end

      def call_method(method_name, class_name=nil)
        name ||= respond_to?(:current_class_name) ? current_class_name : current_module_name
        if methods.has_method?(name, method_name)
          methods.get_method(name, method_name).mark_used
        end
        methods.mark_parent_class_method_used(name, method_name)
        methods.mark_subclasses_method_used(name, method_name)
        methods.possible_public_used(method_name)
      end
  end
end