From 822a906f68664f61a1209336bb681077692c8475 Mon Sep 17 00:00:00 2001 From: zhouguangming Date: Tue, 9 Apr 2013 17:04:47 +0800 Subject: [PATCH] Add nil! method for nil JSON --- lib/jbuilder.rb | 11 ++++++++++- test/jbuilder_test.rb | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/jbuilder.rb b/lib/jbuilder.rb index 728124a..f96bc99 100644 --- a/lib/jbuilder.rb +++ b/lib/jbuilder.rb @@ -73,6 +73,7 @@ def initialize(*args) BLANK = ::Object.new def set!(key, value = BLANK, *args, &block) + result = if ::Kernel.block_given? if BLANK != value # json.comments @post.comments { |comment| ... } @@ -256,6 +257,14 @@ def extract!(object, *attributes) end end + # Returns the nil JSON. + def nil! + @attributes = nil + end + + alias_method :null!, :nil! + + def call(object = nil, *attributes) if attributes.empty? array! object, &::Proc.new @@ -310,4 +319,4 @@ def _merge(hash_or_array) end require 'jbuilder/jbuilder_template' if defined?(ActionView::Template) -require 'jbuilder/railtie' if defined?(Rails::VERSION::MAJOR) && Rails::VERSION::MAJOR == 4 \ No newline at end of file +require 'jbuilder/railtie' if defined?(Rails::VERSION::MAJOR) && Rails::VERSION::MAJOR == 4 diff --git a/test/jbuilder_test.rb b/test/jbuilder_test.rb index 154f814..de486e0 100644 --- a/test/jbuilder_test.rb +++ b/test/jbuilder_test.rb @@ -571,4 +571,10 @@ def empty? assert_equal ['name'], json.attributes!.keys Jbuilder.send(:class_variable_set, '@@ignore_nil', false) end + + test "nil!" do + json = Jbuilder.new + json.nil! + assert_equal nil, json.attributes! + end end