From cf2405998b7fc3d142a935ec15b7f361209bd157 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sun, 12 Jul 2015 01:39:57 +0200 Subject: [PATCH] Ruby::Generator support for #[] methods --- lib/oga/ruby/generator.rb | 10 ++++++---- spec/oga/ruby/generator_spec.rb | 9 +++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/oga/ruby/generator.rb b/lib/oga/ruby/generator.rb index 2f80cf0..deeac5e 100644 --- a/lib/oga/ruby/generator.rb +++ b/lib/oga/ruby/generator.rb @@ -126,15 +126,17 @@ end def on_send(ast) receiver, name, *args = *ast - call = name.dup + call = name + brackets = name == '[]' unless args.empty? - arg_strs = args.map { |arg| process(arg) } - call = "#{call}(#{arg_strs.join(', ')})" + arg_str = args.map { |arg| process(arg) }.join(', ') + call = brackets ? "[#{arg_str}]" : "#{call}(#{arg_str})" end if receiver - call = "#{process(receiver)}.#{call}" + rec_str = process(receiver) + call = brackets ? "#{rec_str}#{call}" : "#{rec_str}.#{call}" end call diff --git a/spec/oga/ruby/generator_spec.rb b/spec/oga/ruby/generator_spec.rb index 73d3b86..6cedd24 100644 --- a/spec/oga/ruby/generator_spec.rb +++ b/spec/oga/ruby/generator_spec.rb @@ -104,6 +104,15 @@ end @generator.on_send(node).should == 'number.foobar(10)' end end + + describe 'when calling #[]' do + it 'returns a correctly formatted String' do + arg = Oga::Ruby::Node.new(:lit, %w{10}) + node = Oga::Ruby::Node.new(:lit, %w{number})[arg] + + @generator.on_send(node).should == 'number[10]' + end + end end describe '#on_block' do