From c98ba21a8769e8328b8488ba7c88c3a7a8ff023c Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sun, 26 Jul 2015 23:20:43 +0200 Subject: [PATCH] Ruby generator support for mass assignments --- lib/oga/ruby/generator.rb | 15 +++++++++++++++ spec/oga/ruby/generator_spec.rb | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/oga/ruby/generator.rb b/lib/oga/ruby/generator.rb index e058f49..ec31908 100644 --- a/lib/oga/ruby/generator.rb +++ b/lib/oga/ruby/generator.rb @@ -41,6 +41,21 @@ module Oga "#{var_str} = #{val_str}" end + ## + # Processes a mass assignment node. + # + # @param [Oga::Ruby::Node] ast + # @return [String] + # + def on_massign(ast) + vars, val = *ast + + var_names = vars.map { |var| process(var) } + val_str = process(val) + + "#{var_names.join(', ')} = #{val_str}" + end + ## # Processes an equality node. # diff --git a/spec/oga/ruby/generator_spec.rb b/spec/oga/ruby/generator_spec.rb index 60c2540..f143665 100644 --- a/spec/oga/ruby/generator_spec.rb +++ b/spec/oga/ruby/generator_spec.rb @@ -25,6 +25,18 @@ describe Oga::Ruby::Generator do end end + describe '#on_mass_assign' do + it 'returns a String' do + var1 = Oga::Ruby::Node.new(:lit, %w{foo}) + var2 = Oga::Ruby::Node.new(:lit, %w{bar}) + val = Oga::Ruby::Node.new(:lit, %w{10}) + + assign = Oga::Ruby::Node.new(:massign, [[var1, var2], val]) + + @generator.on_massign(assign).should == 'foo, bar = 10' + end + end + describe '#on_eq' do it 'returns a String' do var = Oga::Ruby::Node.new(:lit, %w{number})