diff --git a/lib/oga/ruby/generator.rb b/lib/oga/ruby/generator.rb index 1946961..c15aae6 100644 --- a/lib/oga/ruby/generator.rb +++ b/lib/oga/ruby/generator.rb @@ -52,6 +52,22 @@ module Oga "#{var_names.join(', ')} = #{val_str}" end + ## + # Processes a `begin` node. + # + # @param [Oga::Ruby::Node] ast + # @return [String] + # + def on_begin(ast) + body = process(ast.to_a[0]) + + <<-EOF +begin + #{body} +end + EOF + end + ## # Processes an equality node. # diff --git a/lib/oga/ruby/node.rb b/lib/oga/ruby/node.rb index 7e2c0dd..bf11e0e 100644 --- a/lib/oga/ruby/node.rb +++ b/lib/oga/ruby/node.rb @@ -104,6 +104,15 @@ module Oga Node.new(:block, [self, args, yield]) end + ## + # Wraps the current node in a `begin` node. + # + # @return [Oga::Ruby::Node] + # + def wrap + Node.new(:begin, [self]) + end + ## # Wraps the current node in an if statement node. # diff --git a/spec/oga/ruby/generator_spec.rb b/spec/oga/ruby/generator_spec.rb index ca046c4..7d531c7 100644 --- a/spec/oga/ruby/generator_spec.rb +++ b/spec/oga/ruby/generator_spec.rb @@ -37,6 +37,19 @@ describe Oga::Ruby::Generator do end end + describe '#on_begin' do + it 'returns a String' do + number = Oga::Ruby::Node.new(:lit, %w{10}) + node = Oga::Ruby::Node.new(:begin, [number]) + + @generator.on_begin(node).should == <<-EOF +begin + 10 +end + EOF + end + end + describe '#on_eq' do it 'returns a String' do var = Oga::Ruby::Node.new(:lit, %w{number}) diff --git a/spec/oga/ruby/node_spec.rb b/spec/oga/ruby/node_spec.rb index 9eca407..c13dd20 100644 --- a/spec/oga/ruby/node_spec.rb +++ b/spec/oga/ruby/node_spec.rb @@ -88,6 +88,16 @@ describe Oga::Ruby::Node do end end + describe '#wrap' do + it 'returns a begin Node' do + number = described_class.new(:lit, %w{10}) + wrapped = number.wrap + + wrapped.type.should == :begin + wrapped.to_a.should == [number] + end + end + describe '#if_true' do it 'returns an if statement Node' do condition = described_class.new(:lit, %w{number})