Skip to content

Commit b63e7b1

Browse files
committed
Optimize set! DSL
1 parent fddb7ca commit b63e7b1

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

lib/jbuilder.rb

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/jbuilder/jbuilder_template.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ def array!(collection = EMPTY_ARRAY, *args, &block)
131131
end
132132
end
133133

134-
def set!(name, object = BLANK, *args)
134+
def set!(name, object = BLANK, *args, &block)
135135
options = args.first
136136

137-
if args.one? && _partial_options?(options)
137+
if _partial_options?(options)
138138
_set_inline_partial name, object, options.dup
139139
else
140-
super
140+
_set name, object, args, &block
141141
end
142142
end
143143

0 commit comments

Comments
 (0)