Remove useless &block and use yield instead

This commit is contained in:
ccocchi 2012-04-17 16:23:08 +02:00
parent 7767a708df
commit f9e2f0766b
1 changed files with 13 additions and 19 deletions

View File

@ -18,15 +18,6 @@ module RablFastJson
@template @template
end 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 # Sets the object to be used as the data for the template
# Example: # Example:
@ -78,14 +69,14 @@ module RablFastJson
# child(:@posts, :root => :posts) { attribute :id } # child(:@posts, :root => :posts) { attribute :id }
# child(:posts, :partial => 'posts/base') # 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) data, name = extract_data_and_name(name_or_data)
name = options[:root] if options[:root] name = options[:root] if options[:root]
if options[:partial] if options[:partial]
template = Library.instance.get(options[:partial]) template = Library.instance.get(options[:partial])
@template[name] = template.merge!(:_data => data) @template[name] = template.merge!(:_data => data)
else elsif block_given?
_compile_sub_template(name, data, &block) @template[name] = sub_compile(data) { yield }
end end
end end
@ -94,11 +85,11 @@ module RablFastJson
# Example: # Example:
# glue(:@user) { attribute :name } # glue(:@user) { attribute :name }
# #
def glue(data, &block) def glue(data)
return unless block_given? return unless block_given?
name = :"_glue#{@glue_count}" name = :"_glue#{@glue_count}"
@glue_count += 1 @glue_count += 1
_compile_sub_template(name, data, &block) @template[name] = sub_compile(data) { yield }
end end
# #
@ -156,11 +147,14 @@ module RablFastJson
name_or_data name_or_data
end end
end end
def _compile_sub_template(name, data, &block) #:nodoc: def sub_compile(data)
compiler = Compiler.new return {} unless block_given?
template = compiler.compile_block(&block) old_template, @template = @template, {}
@template[name] = template.merge!(:_data => data) yield
@template.merge!(:_data => data)
ensure
@template = old_template
end end
end end
end end