Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def self.encode(*args, &block)
BLANK = Blank.new
NON_ENUMERABLES = [ ::Struct, ::OpenStruct ].to_set

def set!(key, value = BLANK, *args)
def set!(key, value = BLANK, *args, &block)
result = if ::Kernel.block_given?
if !_blank?(value)
# json.comments @post.comments { |comment| ... }
# { "comments": [ { ... }, { ... } ] }
_scope{ array! value, &::Proc.new }
_scope{ array! value, &block }
else
# json.comments { ... }
# { "comments": ... }
Expand Down Expand Up @@ -61,9 +61,9 @@ def set!(key, value = BLANK, *args)
_set_value key, result
end

def method_missing(*args)
def method_missing(*args, &block)
if ::Kernel.block_given?
set!(*args, &::Proc.new)
set!(*args, &block)
else
set!(*args)
end
Expand Down Expand Up @@ -181,11 +181,11 @@ def child!
# json.array! [1, 2, 3]
#
# [1,2,3]
def array!(collection = [], *attributes)
def array!(collection = [], *attributes, &block)
array = if collection.nil?
[]
elsif ::Kernel.block_given?
_map_collection(collection, &::Proc.new)
_map_collection(collection, &block)
elsif attributes.any?
_map_collection(collection) { |element| extract! element, *attributes }
else
Expand Down Expand Up @@ -220,9 +220,9 @@ def extract!(object, *attributes)
end
end

def call(object, *attributes)
def call(object, *attributes, &block)
if ::Kernel.block_given?
array! object, &::Proc.new
array! object, &block
else
extract! object, *attributes
end
Expand Down
4 changes: 2 additions & 2 deletions lib/jbuilder/jbuilder_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def cache_root!(key=nil, options={})
# json.cache_if! !admin?, @person, expires_in: 10.minutes do
# json.extract! @person, :name, :age
# end
def cache_if!(condition, *args)
condition ? cache!(*args, &::Proc.new) : yield
def cache_if!(condition, *args, &block)
condition ? cache!(*args, &block) : yield
end

def target!
Expand Down