Skip to content

Instantly share code, notes, and snippets.

@bluerabbit
Last active August 20, 2020 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluerabbit/3181b804b50804638075f8eb0360ebe1 to your computer and use it in GitHub Desktop.
Save bluerabbit/3181b804b50804638075f8eb0360ebe1 to your computer and use it in GitHub Desktop.
rubyでmethodの前後に文字列を差し込む
# bundle exec ruby-rewrite -l method_rewriter.rb -m app.rb
class MethodRewriter < Parser::TreeRewriter
def on_def(node)
padding = ' ' * (node.location.column + 2)
insert_start(node: node, args: node.children[1].location.expression, code: node.children[2], padding: padding)
insert_after(node.children.last.location.expression, "\n#{padding}#{puts_end}")
super
end
private
def insert_start(node:, args:, code:, padding:)
return unless code
method_name = node.children.first.to_s
insert_after end_definition(node, args), "\n#{padding}#{puts_start(method_name)}"
end
def end_definition(node, args)
args || node.location.name
end
def puts_start(method_name)
"# $action_counter.audit('helper:#{method_name}') do"
end
def puts_end
'# end # action counter'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment