1+ # frozen_string_literal: true
2+
13require 'active_support'
24require 'jbuilder/jbuilder'
35require 'jbuilder/blank'
@@ -12,14 +14,18 @@ class Jbuilder
1214 @@ignore_nil = false
1315 @@deep_format_keys = false
1416
15- def initialize ( options = { } )
17+ def initialize (
18+ key_formatter : @@key_formatter ,
19+ ignore_nil : @@ignore_nil ,
20+ deep_format_keys : @@deep_format_keys ,
21+ &block
22+ )
1623 @attributes = { }
24+ @key_formatter = key_formatter
25+ @ignore_nil = ignore_nil
26+ @deep_format_keys = deep_format_keys
1727
18- @key_formatter = options . fetch ( :key_formatter ) { @@key_formatter ? @@key_formatter . clone : nil }
19- @ignore_nil = options . fetch ( :ignore_nil , @@ignore_nil )
20- @deep_format_keys = options . fetch ( :deep_format_keys , @@deep_format_keys )
21-
22- yield self if ::Kernel . block_given?
28+ yield self if block
2329 end
2430
2531 # Yields a builder and automatically turns the result into a JSON string
@@ -58,7 +64,7 @@ def set!(key, value = BLANK, *args, &block)
5864 else
5965 # json.author @post.creator, :name, :email_address
6066 # { "author": { "name": "David", "email_address": "[email protected] " } } 61- _merge_block ( key ) { extract! value , * args }
67+ _merge_block ( key ) { _extract value , args }
6268 end
6369
6470 _set_value key , result
@@ -100,13 +106,13 @@ def method_missing(*args, &block)
100106 #
101107 # { "_first_name": "David" }
102108 #
103- def key_format! ( * args )
104- @key_formatter = KeyFormatter . new ( * args )
109+ def key_format! ( ... )
110+ @key_formatter = KeyFormatter . new ( ... )
105111 end
106112
107113 # Same as the instance method key_format! except sets the default.
108- def self . key_format ( * args )
109- @@key_formatter = KeyFormatter . new ( * args )
114+ def self . key_format ( ... )
115+ @@key_formatter = KeyFormatter . new ( ... )
110116 end
111117
112118 # If you want to skip adding nil values to your JSON hash. This is useful
@@ -215,7 +221,7 @@ def array!(collection = [], *attributes, &block)
215221 elsif ::Kernel . block_given?
216222 _map_collection ( collection , &block )
217223 elsif attributes . any?
218- _map_collection ( collection ) { |element | extract! element , * attributes }
224+ _map_collection ( collection ) { |element | _extract element , attributes }
219225 else
220226 _format_keys ( collection . to_a )
221227 end
@@ -241,18 +247,14 @@ def array!(collection = [], *attributes, &block)
241247 #
242248 # json.(@person, :name, :age)
243249 def extract! ( object , *attributes )
244- if ::Hash === object
245- _extract_hash_values ( object , attributes )
246- else
247- _extract_method_values ( object , attributes )
248- end
250+ _extract object , attributes
249251 end
250252
251253 def call ( object , *attributes , &block )
252254 if ::Kernel . block_given?
253255 array! object , &block
254256 else
255- extract! object , * attributes
257+ _extract object , attributes
256258 end
257259 end
258260
@@ -281,6 +283,14 @@ def target!
281283
282284 private
283285
286+ def _extract ( object , attributes )
287+ if ::Hash === object
288+ _extract_hash_values ( object , attributes )
289+ else
290+ _extract_method_values ( object , attributes )
291+ end
292+ end
293+
284294 def _extract_hash_values ( object , attributes )
285295 attributes . each { |key | _set_value key , _format_keys ( object . fetch ( key ) ) }
286296 end
@@ -311,7 +321,13 @@ def _merge_values(current_value, updates)
311321 end
312322
313323 def _key ( key )
314- @key_formatter ? @key_formatter . format ( key ) : key . to_s
324+ if @key_formatter
325+ @key_formatter . format ( key )
326+ elsif key . is_a? ( ::Symbol )
327+ key . name
328+ else
329+ key . to_s
330+ end
315331 end
316332
317333 def _format_keys ( hash_or_array )
@@ -350,16 +366,12 @@ def _scope
350366 end
351367
352368 def _is_collection? ( object )
353- _object_respond_to? ( object , :map , :count ) && !( ::Struct === object )
369+ object . respond_to? ( :map ) && object . respond_to? ( :count ) && !( ::Struct === object )
354370 end
355371
356372 def _blank? ( value = @attributes )
357373 BLANK == value
358374 end
359-
360- def _object_respond_to? ( object , *methods )
361- methods . all? { |m | object . respond_to? ( m ) }
362- end
363375end
364376
365377require 'jbuilder/railtie' if defined? ( Rails )
0 commit comments