77require 'jbuilder/errors'
88require 'json'
99require 'active_support/core_ext/hash/deep_merge'
10+ require 'active_support/core_ext/object/blank'
1011
1112class Jbuilder
1213 @@key_formatter = nil
@@ -32,14 +33,16 @@ def self.encode(...)
3233 new ( ...) . target!
3334 end
3435
35- BLANK = Blank . new
36+ BLANK = Blank . new . freeze
37+ EMPTY_ARRAY = [ ] . freeze
38+ private_constant :BLANK , :EMPTY_ARRAY
3639
3740 def set! ( key , value = BLANK , *args , &block )
3841 result = if ::Kernel . block_given?
3942 if !_blank? ( value )
4043 # json.comments @post.comments { |comment| ... }
4144 # { "comments": [ { ... }, { ... } ] }
42- _scope { array! value , &block }
45+ _scope { _array value , &block }
4346 else
4447 # json.comments { ... }
4548 # { "comments": ... }
@@ -59,7 +62,7 @@ def set!(key, value = BLANK, *args, &block)
5962 elsif _is_collection? ( value )
6063 # json.comments @post.comments, :content, :created_at
6164 # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
62- _scope { array! value , * args }
65+ _scope { _array value , args }
6366 else
6467 # json.author @post.creator, :name, :email_address
6568 # { "author": { "name": "David", "email_address": "[email protected] " } } @@ -206,18 +209,8 @@ def child!
206209 # json.array! [1, 2, 3]
207210 #
208211 # [1,2,3]
209- def array! ( collection = [ ] , *attributes , &block )
210- array = if collection . nil?
211- [ ]
212- elsif ::Kernel . block_given?
213- _map_collection ( collection , &block )
214- elsif attributes . any?
215- _map_collection ( collection ) { |element | _extract element , attributes }
216- else
217- _format_keys ( collection . to_a )
218- end
219-
220- @attributes = _merge_values ( @attributes , array )
212+ def array! ( collection = EMPTY_ARRAY , *attributes , &block )
213+ _array collection , attributes , &block
221214 end
222215
223216 # Extracts the mentioned attributes or hash elements from the passed object and turns them into attributes of the JSON.
@@ -243,7 +236,7 @@ def extract!(object, *attributes)
243236
244237 def call ( object , *attributes , &block )
245238 if ::Kernel . block_given?
246- array! object , &block
239+ _array object , &block
247240 else
248241 _extract object , attributes
249242 end
@@ -276,6 +269,20 @@ def target!
276269
277270 alias_method :method_missing , :set!
278271
272+ def _array ( collection = EMPTY_ARRAY , attributes = nil , &block )
273+ array = if collection . nil?
274+ EMPTY_ARRAY
275+ elsif block
276+ _map_collection ( collection , &block )
277+ elsif attributes . present?
278+ _map_collection ( collection ) { |element | _extract element , attributes }
279+ else
280+ _format_keys ( collection . to_a )
281+ end
282+
283+ @attributes = _merge_values ( @attributes , array )
284+ end
285+
279286 def _extract ( object , attributes )
280287 if ::Hash === object
281288 _extract_hash_values ( object , attributes )
0 commit comments