Created
May 18, 2018 03:45
-
-
Save jonatas/e70c874cbbd0cac5a9abd9f4a78fa816 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'ffast', | |
git: 'https://github.com/jonatas/fast.git', | |
require: 'fast' | |
end | |
[ | |
'client.read_all_streams_forward(count: 1)', | |
'client.read_all_streams_forward(count: 1, start: :head)' | |
].each do |sample| | |
ast = Fast.ast(sample) | |
new_code = Fast.replace ast, | |
'(send (send nil client) $_ $... )', | |
->(node, captures) { | |
method, args = captures | |
new_method_call = 'read' | |
new_method_call << '.backward' if method == :read_all_streams_backward | |
key_values = args.children.map { |e| Fast.capture(e, '(pair (sym $_) (_ $_)))') } | |
key_values.each do |key, value| | |
new_method_call << '.' | |
new_method_call << case key | |
when :count | |
'limit' | |
when :start | |
'from' | |
else | |
key.to_s | |
end | |
new_method_call << "(#{value.inspect})" | |
end | |
replace_range = node.location.selector | |
replace_range = replace_range.join(node.location.end) if node.location.end | |
replace(replace_range, new_method_call) | |
} | |
puts "#{sample} => #{new_code}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment