Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -12,12 +12,12 @@ class Jbuilder
@@ignore_nil = false
@@deep_format_keys = false

def initialize(options = {})
def initialize(options = nil)
@attributes = {}

@key_formatter = options.fetch(:key_formatter){ @@key_formatter ? @@key_formatter.clone : nil}
@ignore_nil = options.fetch(:ignore_nil, @@ignore_nil)
@deep_format_keys = options.fetch(:deep_format_keys, @@deep_format_keys)
@key_formatter = options&.[](:key_formatter) || @@key_formatter
@ignore_nil = options&.[](:ignore_nil) || @@ignore_nil
@deep_format_keys = options&.[](:deep_format_keys) || @@deep_format_keys

yield self if ::Kernel.block_given?
end
Expand Down Expand Up @@ -100,13 +100,13 @@ def method_missing(*args, &block)
#
# { "_first_name": "David" }
#
def key_format!(*args)
@key_formatter = KeyFormatter.new(*args)
def key_format!(...)
@key_formatter = KeyFormatter.new(...)
end

# Same as the instance method key_format! except sets the default.
def self.key_format(*args)
@@key_formatter = KeyFormatter.new(*args)
def self.key_format(...)
@@key_formatter = KeyFormatter.new(...)
end

# If you want to skip adding nil values to your JSON hash. This is useful
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 @@ -10,10 +10,10 @@ class << self

self.template_lookup_options = { handlers: [:jbuilder] }

def initialize(context, *args)
def initialize(context, options = nil)
@context = context
@cached_root = nil
super(*args)
super(options)
end

# Generates JSON using the template specified with the `:partial` option. For example, the code below will render
Expand Down
36 changes: 14 additions & 22 deletions lib/jbuilder/key_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
require 'jbuilder/jbuilder'
require 'active_support/core_ext/array'

class Jbuilder
class KeyFormatter
def initialize(*args)
@format = {}
@cache = {}
def initialize(*formats, **formats_with_options)
@cache =
Hash.new do |hash, key|
value = key.is_a?(Symbol) ? key.name : key.to_s

options = args.extract_options!
args.each do |name|
@format[name] = []
end
options.each do |name, parameters|
@format[name] = parameters
end
end
formats.each do |func|
value = func.is_a?(Proc) ? func.call(value) : value.send(func)
end

formats_with_options.each do |func, params|
value = func.is_a?(Proc) ? func.call(value, *params) : value.send(func, *params)
end

def initialize_copy(original)
@cache = {}
hash[key] = value
end
end

def format(key)
@cache[key] ||= @format.inject(key.to_s) do |result, args|
func, args = args
if ::Proc === func
func.call result, *args
else
result.send func, *args
end
end
@cache[key]
end
end
end
8 changes: 0 additions & 8 deletions test/jbuilder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -784,14 +784,6 @@ class JbuilderTest < ActiveSupport::TestCase
assert_equal ['camelStyle'], result.keys
end

test 'do not use default key formatter directly' do
Jbuilder.key_format
jbuild{ |json| json.key 'value' }
formatter = Jbuilder.send(:class_variable_get, '@@key_formatter')
cache = formatter.instance_variable_get('@cache')
assert_empty cache
end

test 'ignore_nil! without a parameter' do
result = jbuild do |json|
json.ignore_nil!
Expand Down