Skip to content

Commit

Permalink
update readme with key formatting (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
kortirso authored Jul 30, 2024
1 parent ad4729c commit f9e640f
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ You can also add a [layout](#layouts).

### json.set! or json.\<your key here\>

Defines the attribute or structure. All keys are automatically camelized lower
by default. See [Change Key Format](#change-key-format) to change this behavior.
Defines the attribute or structure. All keys are not formatted by default. See [Change Key Format](#change-key-format) to change this behavior.

```ruby
json.set! :authorDetails, {...options} do
Expand Down Expand Up @@ -291,7 +290,7 @@ end
### Partials

Partials are supported. The following will render the file
`views/posts/_blog_posts.json.props`, and set a local variable `foo` assigned
`views/posts/_blog_posts.json.props`, and set a local variable `post` assigned
with @post, which you can use inside the partial.

```ruby
Expand All @@ -303,6 +302,7 @@ Usage with arrays:

```ruby
# The `as:` option is supported when using `array!`
# Without `as:` option you can use blog_post variable (name is based on partial's name) inside partial

json.posts do
json.array! @posts, partial: ["posts/blog_post", locals: {foo: 'bar'}, as: 'post'] do
Expand Down Expand Up @@ -589,11 +589,38 @@ By default, keys are not formatted. If you want to change this behavior,
override it in an initializer:

```ruby
# default behavior
Props::BaseWithExtensions.class_eval do
# json.firstValue "first"
# json.second_value "second"
#
# -> { "firstValue" => "first", "second_value" => "second" }
def key_format(key)
key.to_s
end
end

# camelCased behavior
Props::BaseWithExtensions.class_eval do
# json.firstValue "first"
# json.second_value "second"
#
# -> { "firstValue" => "first", "secondValue" => "second" }
def key_format(key)
key.to_s.camelize(:lower)
end
end

# snake_cased behavior
Props::BaseWithExtensions.class_eval do
# json.firstValue "first"
# json.second_value "second"
#
# -> { "first_value" => "first", "second_value" => "second" }
def key_format(key)
key.to_s.underscore
end
end
```

## Escape mode
Expand Down

0 comments on commit f9e640f

Please sign in to comment.