Class: Torikago::Checker::GatewayCallExtractor
- Inherits:
-
Object
- Object
- Torikago::Checker::GatewayCallExtractor
- Defined in:
- lib/torikago/checker.rb,
sig/torikago.rbs
Instance Attribute Summary collapse
-
#calls ⇒ Array[GatewayCall]
readonly
Returns the value of attribute calls.
-
#dynamic_call_count ⇒ Integer
readonly
Returns the value of attribute dynamic_call_count.
-
#sexp ⇒ Object
readonly
Returns the value of attribute sexp.
Instance Method Summary collapse
- #arguments_from(node) ⇒ Array[untyped]
- #build_call?(node) ⇒ Boolean
- #call ⇒ GatewayCallExtractor
- #constant_name(node) ⇒ String?
- #extract(node) ⇒ void
- #gateway_constant?(node) ⇒ Boolean
-
#initialize(source) ⇒ GatewayCallExtractor
constructor
A new instance of GatewayCallExtractor.
- #record(class_name, method_name) ⇒ Object
- #string_literal(node) ⇒ String?
- #symbol_literal(node) ⇒ Symbol?
- #token_value(token) ⇒ String?
- #walk(node) ⇒ void
Constructor Details
#initialize(source) ⇒ GatewayCallExtractor
Returns a new instance of GatewayCallExtractor.
31 32 33 34 35 |
# File 'lib/torikago/checker.rb', line 31 def initialize(source) @sexp = Ripper.sexp(source) @calls = Array.new @dynamic_call_count = 0 end |
Instance Attribute Details
#calls ⇒ Array[GatewayCall] (readonly)
Returns the value of attribute calls.
29 30 31 |
# File 'lib/torikago/checker.rb', line 29 def calls @calls end |
#dynamic_call_count ⇒ Integer (readonly)
Returns the value of attribute dynamic_call_count.
29 30 31 |
# File 'lib/torikago/checker.rb', line 29 def dynamic_call_count @dynamic_call_count end |
#sexp ⇒ Object (readonly)
Returns the value of attribute sexp.
44 45 46 |
# File 'lib/torikago/checker.rb', line 44 def sexp @sexp end |
Instance Method Details
#arguments_from(node) ⇒ Array[untyped]
101 102 103 104 105 106 107 108 |
# File 'lib/torikago/checker.rb', line 101 def arguments_from(node) return Array.new unless node&.first == :arg_paren args_add_block = node[1] return Array.new unless args_add_block&.first == :args_add_block args_add_block[1] || Array.new end |
#build_call?(node) ⇒ Boolean
77 78 79 80 81 82 83 84 |
# File 'lib/torikago/checker.rb', line 77 def build_call?(node) return false unless node&.first == :method_add_arg call_node = node[1] call_node&.first == :call && token_value(call_node[3]) == "build" && gateway_constant?(call_node[1]) end |
#call ⇒ GatewayCallExtractor
37 38 39 40 |
# File 'lib/torikago/checker.rb', line 37 def call walk(sexp) self end |
#constant_name(node) ⇒ String?
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/torikago/checker.rb', line 90 def constant_name(node) return unless node.is_a?(Array) case node.first when :var_ref, :const_ref, :top_const_ref token_value(node[1]) when :const_path_ref [constant_name(node[1]), token_value(node[2])].compact.join("::") end end |
#extract(node) ⇒ void
This method returns an undefined value.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/torikago/checker.rb', line 53 def extract(node) call_node = node[1] return unless call_node&.first == :call return unless token_value(call_node[3]) == "invoke" receiver = call_node[1] arguments = arguments_from(node[2]) if gateway_constant?(receiver) record(string_literal(arguments[0]), symbol_literal(arguments[1])) elsif build_call?(receiver) build_arguments = arguments_from(receiver[2]) record(string_literal(build_arguments[0]), symbol_literal(arguments[0])) end end |
#gateway_constant?(node) ⇒ Boolean
86 87 88 |
# File 'lib/torikago/checker.rb', line 86 def gateway_constant?(node) constant_name(node) == "Torikago::Gateway" end |
#record(class_name, method_name) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/torikago/checker.rb', line 69 def record(class_name, method_name) if class_name && method_name calls << GatewayCall.new(class_name: class_name, method_name: method_name) else @dynamic_call_count += 1 end end |
#string_literal(node) ⇒ String?
110 111 112 113 114 115 116 117 118 |
# File 'lib/torikago/checker.rb', line 110 def string_literal(node) return unless node&.first == :string_literal content = node[1] parts = content&.first == :string_content ? content.drop(1) : Array.new return unless parts.length == 1 && parts.first&.first == :@tstring_content token_value(parts.first) end |
#symbol_literal(node) ⇒ Symbol?
120 121 122 123 124 125 126 |
# File 'lib/torikago/checker.rb', line 120 def symbol_literal(node) return unless node&.first == :symbol_literal symbol = node[1] token = symbol&.first == :symbol ? symbol[1] : nil token_value(token)&.to_sym end |
#token_value(token) ⇒ String?
128 129 130 |
# File 'lib/torikago/checker.rb', line 128 def token_value(token) token[1] if token.is_a?(Array) && token.first.to_s.start_with?("@") end |
#walk(node) ⇒ void
This method returns an undefined value.
46 47 48 49 50 51 |
# File 'lib/torikago/checker.rb', line 46 def walk(node) return unless node.is_a?(Array) extract(node) if node.first == :method_add_arg node.each { |child| walk(child) if child.is_a?(Array) } end |