Skip to content

Instantly share code, notes, and snippets.

@jonatas
Created May 18, 2018 03:45
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 jonatas/e70c874cbbd0cac5a9abd9f4a78fa816 to your computer and use it in GitHub Desktop.
Save jonatas/e70c874cbbd0cac5a9abd9f4a78fa816 to your computer and use it in GitHub Desktop.
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