From f9e2f0766b3c2089fedebf9622b25dce79c3eaee Mon Sep 17 00:00:00 2001 From: ccocchi Date: Tue, 17 Apr 2012 16:23:08 +0200 Subject: [PATCH] Remove useless &block and use yield instead --- lib/rabl-fast-json/compiler.rb | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/lib/rabl-fast-json/compiler.rb b/lib/rabl-fast-json/compiler.rb index a8422d1..8619b26 100644 --- a/lib/rabl-fast-json/compiler.rb +++ b/lib/rabl-fast-json/compiler.rb @@ -18,15 +18,6 @@ module RablFastJson @template end - # - # Same as compile_source but from a block - # - def compile_block(&block) - @template = {} - instance_eval(&block) - @template - end - # # Sets the object to be used as the data for the template # Example: @@ -78,14 +69,14 @@ module RablFastJson # child(:@posts, :root => :posts) { attribute :id } # child(:posts, :partial => 'posts/base') # - def child(name_or_data, options = {}, &block) + def child(name_or_data, options = {}) data, name = extract_data_and_name(name_or_data) name = options[:root] if options[:root] if options[:partial] template = Library.instance.get(options[:partial]) @template[name] = template.merge!(:_data => data) - else - _compile_sub_template(name, data, &block) + elsif block_given? + @template[name] = sub_compile(data) { yield } end end @@ -94,11 +85,11 @@ module RablFastJson # Example: # glue(:@user) { attribute :name } # - def glue(data, &block) + def glue(data) return unless block_given? name = :"_glue#{@glue_count}" @glue_count += 1 - _compile_sub_template(name, data, &block) + @template[name] = sub_compile(data) { yield } end # @@ -156,11 +147,14 @@ module RablFastJson name_or_data end end - - def _compile_sub_template(name, data, &block) #:nodoc: - compiler = Compiler.new - template = compiler.compile_block(&block) - @template[name] = template.merge!(:_data => data) + + def sub_compile(data) + return {} unless block_given? + old_template, @template = @template, {} + yield + @template.merge!(:_data => data) + ensure + @template = old_template end end end \ No newline at end of file