Skip to content

Commit

Permalink
Additional implementation details for key override (#35)
Browse files Browse the repository at this point in the history
This adds a helpful note to the change key format section in case someone
REALLY wants to change the way keys are formatted.
  • Loading branch information
jho406 authored Aug 2, 2024
1 parent f9e640f commit f671628
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,10 @@ json.flash flash.to_h
will render Layout first, then the template when `yield json` is used.

## Change key format
By default, keys are not formatted. If you want to change this behavior,
override it in an initializer:
By default, keys are not formatted. This is intentional. By being explicity with your keys,
it makes your views quicker and more easily searchable when working in Javascript land.

If you must change this behavior, override it in an initializer and cache the value:

```ruby
# default behavior
Expand All @@ -607,7 +609,15 @@ Props::BaseWithExtensions.class_eval do
#
# -> { "firstValue" => "first", "secondValue" => "second" }
def key_format(key)
key.to_s.camelize(:lower)
@key_cache ||= {}
@key_cache[key] ||= key.to_s.camelize(:lower)
@key_cache[key]
end

def result!
result = super
@key_cache = {}
result
end
end

Expand All @@ -618,7 +628,15 @@ Props::BaseWithExtensions.class_eval do
#
# -> { "first_value" => "first", "second_value" => "second" }
def key_format(key)
key.to_s.underscore
@key_cache ||= {}
@key_cache[key] ||= key.to_s.underscore
@key_cache[key]
end

def result!
result = super
@key_cache = {}
result
end
end
```
Expand Down

0 comments on commit f671628

Please sign in to comment.