Module: ImproveYourCode::AST::SexpExtensions::SendNode

Defined in:
lib/improve_your_code/ast/sexp_extensions/send.rb

Overview

Utility methods for :send nodes.

Constant Summary collapse

ATTR_DEFN_METHODS =
%i[attr_writer attr_accessor].freeze

Instance Method Summary collapse

Instance Method Details

#argsObject



18
19
20
# File 'lib/improve_your_code/ast/sexp_extensions/send.rb', line 18

def args
  children[2..-1]
end

#attr_with_writable_flag?Boolean

Handles the case where we create an attribute writer via: attr :foo, true

Returns:

  • (Boolean)


46
47
48
# File 'lib/improve_your_code/ast/sexp_extensions/send.rb', line 46

def attr_with_writable_flag?
  name == :attr && args.any? && args.last.type == :true
end

#attribute_writer?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/improve_your_code/ast/sexp_extensions/send.rb', line 39

def attribute_writer?
  ATTR_DEFN_METHODS.include?(name) ||
    attr_with_writable_flag?
end

#module_creation_call?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/improve_your_code/ast/sexp_extensions/send.rb', line 26

def module_creation_call?
  object_creation_call? && module_creation_receiver?
end

#module_creation_receiver?Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/improve_your_code/ast/sexp_extensions/send.rb', line 30

def module_creation_receiver?
  receiver&.type == :const &&
    %i[Class Struct].include?(receiver.simple_name)
end

#nameObject



14
15
16
# File 'lib/improve_your_code/ast/sexp_extensions/send.rb', line 14

def name
  children[1]
end

#object_creation_call?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/improve_your_code/ast/sexp_extensions/send.rb', line 35

def object_creation_call?
  name == :new
end

#participantsObject



22
23
24
# File 'lib/improve_your_code/ast/sexp_extensions/send.rb', line 22

def participants
  ([receiver] + args).compact
end

#receiverObject



10
11
12
# File 'lib/improve_your_code/ast/sexp_extensions/send.rb', line 10

def receiver
  children.first
end