Ruby generator support for mass assignments

This commit is contained in:
Yorick Peterse 2015-07-26 23:20:43 +02:00
parent 53c142ca26
commit c98ba21a87
2 changed files with 27 additions and 0 deletions

View File

@ -41,6 +41,21 @@ module Oga
"#{var_str} = #{val_str}" "#{var_str} = #{val_str}"
end 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. # Processes an equality node.
# #

View File

@ -25,6 +25,18 @@ describe Oga::Ruby::Generator do
end end
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 describe '#on_eq' do
it 'returns a String' do it 'returns a String' do
var = Oga::Ruby::Node.new(:lit, %w{number}) var = Oga::Ruby::Node.new(:lit, %w{number})