@@ -38,38 +38,7 @@ def self.encode(...)
3838 private_constant :BLANK , :EMPTY_ARRAY
3939
4040 def set! ( key , value = BLANK , *args , &block )
41- result = if ::Kernel . block_given?
42- if !_blank? ( value )
43- # json.comments @post.comments { |comment| ... }
44- # { "comments": [ { ... }, { ... } ] }
45- _scope { _array value , &block }
46- else
47- # json.comments { ... }
48- # { "comments": ... }
49- _merge_block ( key ) { yield self }
50- end
51- elsif args . empty?
52- if ::Jbuilder === value
53- # json.age 32
54- # json.person another_jbuilder
55- # { "age": 32, "person": { ... }
56- _format_keys ( value . attributes! )
57- else
58- # json.age 32
59- # { "age": 32 }
60- _format_keys ( value )
61- end
62- elsif _is_collection? ( value )
63- # json.comments @post.comments, :content, :created_at
64- # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
65- _scope { _array value , args }
66- else
67- # json.author @post.creator, :name, :email_address
68- # { "author": { "name": "David", "email_address": "[email protected] " } } 69- _merge_block ( key ) { _extract value , args }
70- end
71-
72- _set_value key , result
41+ _set ( key , value , args , &block )
7342 end
7443
7544 # Specifies formatting to be applied to the key. Passing in a name of a function
@@ -269,6 +238,41 @@ def target!
269238
270239 alias_method :method_missing , :set!
271240
241+ def _set ( key , value = BLANK , attributes , &block )
242+ result = if block
243+ if _blank? ( value )
244+ # json.comments { ... }
245+ # { "comments": ... }
246+ _merge_block key , &block
247+ else
248+ # json.comments @post.comments { |comment| ... }
249+ # { "comments": [ { ... }, { ... } ] }
250+ _scope { _array value , &block }
251+ end
252+ elsif attributes . empty?
253+ if ::Jbuilder === value
254+ # json.age 32
255+ # json.person another_jbuilder
256+ # { "age": 32, "person": { ... }
257+ _format_keys ( value . attributes! )
258+ else
259+ # json.age 32
260+ # { "age": 32 }
261+ _format_keys ( value )
262+ end
263+ elsif _is_collection? ( value )
264+ # json.comments @post.comments, :content, :created_at
265+ # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
266+ _scope { _array value , attributes }
267+ else
268+ # json.author @post.creator, :name, :email_address
269+ # { "author": { "name": "David", "email_address": "[email protected] " } } 270+ _merge_block ( key ) { _extract value , attributes }
271+ end
272+
273+ _set_value key , result
274+ end
275+
272276 def _array ( collection = EMPTY_ARRAY , attributes = nil , &block )
273277 array = if collection . nil?
274278 EMPTY_ARRAY
0 commit comments