Class: I18n::Processes::Scanners::RubyAstCallFinder
- Inherits:
-
Object
- Object
- I18n::Processes::Scanners::RubyAstCallFinder
- Includes:
- AST::Processor::Mixin
- Defined in:
- lib/i18n/processes/scanners/ruby_ast_call_finder.rb
Instance Method Summary collapse
- #collect_calls(root_node) {|send_node, method_name| ... } ⇒ Array<block return values excluding nils>
- #find_calls(root_node) {|send_node, method_name| ... } ⇒ Object
- #handler_missing(node) ⇒ Object
-
#initialize(receiver_messages:) ⇒ RubyAstCallFinder
constructor
A new instance of RubyAstCallFinder.
- #on_def(node) ⇒ Object
- #on_send(send_node) ⇒ Object
Constructor Details
#initialize(receiver_messages:) ⇒ RubyAstCallFinder
Returns a new instance of RubyAstCallFinder.
10 11 12 13 14 15 |
# File 'lib/i18n/processes/scanners/ruby_ast_call_finder.rb', line 10 def initialize(receiver_messages:) super() = .each_with_object({}) do |(receiver, ), t| (t[] ||= []) << receiver end end |
Instance Method Details
#collect_calls(root_node) {|send_node, method_name| ... } ⇒ Array<block return values excluding nils>
30 31 32 33 34 35 36 37 |
# File 'lib/i18n/processes/scanners/ruby_ast_call_finder.rb', line 30 def collect_calls(root_node) results = [] find_calls root_node do |send_node, method_name| result = yield send_node, method_name results << result if result end results end |
#find_calls(root_node) {|send_node, method_name| ... } ⇒ Object
20 21 22 23 24 25 |
# File 'lib/i18n/processes/scanners/ruby_ast_call_finder.rb', line 20 def find_calls(root_node, &block) @callback = block process root_node ensure @callback = nil end |
#handler_missing(node) ⇒ Object
57 58 59 60 |
# File 'lib/i18n/processes/scanners/ruby_ast_call_finder.rb', line 57 def handler_missing(node) node.children.each { |child| process(child) if child.is_a?(::Parser::AST::Node) } nil end |
#on_def(node) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/i18n/processes/scanners/ruby_ast_call_finder.rb', line 39 def on_def(node) @method_name = node.children[0] handler_missing node ensure @method_name = nil end |
#on_send(send_node) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/i18n/processes/scanners/ruby_ast_call_finder.rb', line 46 def on_send(send_node) receiver = send_node.children[0] = send_node.children[1] valid_receivers = [] # use `any?` because `include?` checks type equality, but the receiver is a Parser::AST::Node != AST::Node. @callback.call(send_node, @method_name) if valid_receivers && valid_receivers.any? { |r| r == receiver } # always invoke handler_missing to get nested translations in children handler_missing send_node nil end |