Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 14 additions & 8 deletions lib/jbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ 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)
if options
@key_formatter = options[:key_formatter]
@ignore_nil = options[:ignore_nil]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My ruby is bad now - will this be nil if it's unset which I know is falsy but..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options can't be unset. It is a required parameter that defaults to nil.

I can change this to

    if options.nil?
      @key_formatter = @@key_formatter
      @ignore_nil = @@ignore_nil
      @deep_format_keys = @@deep_format_keys
    else
      @key_formatter = options[:key_formatter]
      @ignore_nil = options[:ignore_nil]
      @deep_format_keys = options[:deep_format_keys]
    end

to make it clearer (and perhaps safer).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually... I wonder if it's better to just use kwargs here... hold on, I think I got something better.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options can't be unset. It is a required parameter that defaults to nil.

I can change this to

    if options.nil?
      @key_formatter = @@key_formatter
      @ignore_nil = @@ignore_nil
      @deep_format_keys = @@deep_format_keys
    else
      @key_formatter = options[:key_formatter]
      @ignore_nil = options[:ignore_nil]
      @deep_format_keys = options[:deep_format_keys]
    end

to make it clearer (and perhaps safer).

I mean the value in :ignore_nil is a nil if it's not there right?

Copy link
Author

@moberegger moberegger Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Hashes will return nil if the key doesn't exist. Should have been false. Likewise with : deep_format_keys. I was too careless hammering this out.

Moot point. Moved it over to use key args, which actually speeds things up a bit more.

@deep_format_keys = options[:deep_format_keys]
else
@key_formatter = @@key_formatter
@ignore_nil = @@ignore_nil
@deep_format_keys = @@deep_format_keys
end

yield self if ::Kernel.block_given?
end
Expand Down Expand Up @@ -100,13 +106,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
38 changes: 17 additions & 21 deletions lib/jbuilder/key_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
require 'jbuilder/jbuilder'
require 'active_support/core_ext/array'

class Jbuilder
class KeyFormatter
def initialize(*args)
@format = {}
@cache = {}

options = args.extract_options!
args.each do |name|
@format[name] = []
end
options.each do |name, parameters|
@format[name] = parameters
end
end

def initialize_copy(original)
def initialize(*formats, **formats_with_options)
@mutex = Mutex.new
@formats = formats
@formats_with_options = formats_with_options
@cache = {}
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
@mutex.synchronize do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very annoying that ruby doesn't come native with a RW lock (esp an upgradable one) cause this would be a perfect case for one given the expected use patterns. Might be some check-lock-check patterns that might be a bit faster under high contention but not worth exploring atm.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't, but the concurrent gem provides one, and we use it already in the app: https://ruby-concurrency.github.io/concurrent-ruby/1.1.5/Concurrent/ReadWriteLock.html

Copy link

@Insomniak47 Insomniak47 Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't fork and then add deps I don't think and achieve the goal of compat

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I know

@cache[key] ||= begin
value = key.is_a?(Symbol) ? key.name : key.to_s

@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

value
end
end
end
Expand Down
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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe repurpose this to validate that it is used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose I should just repurpose this test. With the Mutex I'm much more confident with the approach.


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