Skip to content

Commit

Permalink
Add nil! method for nil JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouguangming committed Apr 9, 2013
1 parent 37188b3 commit 822a906
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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| ... }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
require 'jbuilder/railtie' if defined?(Rails::VERSION::MAJOR) && Rails::VERSION::MAJOR == 4
6 changes: 6 additions & 0 deletions test/jbuilder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 822a906

Please sign in to comment.