Skip to content

Commit

Permalink
chore: improve readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Mar 27, 2023
1 parent f726658 commit 8da9ef9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ In case you need to pass options, you can call the serializer manually:

```ruby
class SongSerializer < Oj::Serializer
attribute
def album
attribute :album do
AlbumSerializer.one(song.album, for_song: song)
end
end
Expand Down Expand Up @@ -368,7 +367,7 @@ One slight variation that might make it easier to maintain in the long term is
to use a separate singleton service to provide the url helpers and options, and
make it available as `urls`.

### Memoization & Local State
### Memoization & local state

Serializers are designed to be stateless so that an instanced can be reused, but
sometimes it's convenient to store intermediate calculations.
Expand Down Expand Up @@ -404,7 +403,7 @@ class PersonSerializer < Oj::Serializer
end

PersonSerializer.one('first_name' => 'Mary', :middle_name => 'Jane', :last_name => 'Watson')
# {"first_name":"Mary","last_name":"Watson"}
# {first_name: "Mary", last_name: "Watson"}
```

### `mongo_attributes` 🚀
Expand Down
4 changes: 2 additions & 2 deletions lib/oj_serializers/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def options
def _check_instance_variables
if instance_values.keys.any? { |key| !ALLOWED_INSTANCE_VARIABLES.include?(key) }
bad_keys = instance_values.keys.reject { |key| ALLOWED_INSTANCE_VARIABLES.include?(key) }
raise ArgumentError, "Serializer instances are reused so they must be stateless. Use `memo.fetch` for memoization purposes instead. Bad keys: #{bad_keys.join(',')}"
raise ArgumentError, "Serializer instances are reused so they must be stateless. Use `memo.fetch` for memoization purposes instead. Bad keys: #{bad_keys.join(',')} in #{self.class}"
end
end
end
Expand Down Expand Up @@ -532,7 +532,7 @@ def code_to_rescue_no_method
# Internal: Detects any include methods defined in the serializer, or defines
# one by using the lambda passed in the `if` option, if any.
def check_conditional_method(method_name, options)
include_method_name = "include_#{method_name}#{'?' unless method_name.ends_with?('?')}"
include_method_name = "include_#{method_name}#{'?' unless method_name.to_s.ends_with?('?')}"
if render_if = options[:if]
if render_if.is_a?(Symbol)
alias_method(include_method_name, render_if)
Expand Down
2 changes: 1 addition & 1 deletion spec/oj_serializers/dev_mode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MissingAttributeSerializer < Oj::Serializer

it 'should fail early when memoization is used incorrectly' do
expect { StatefulSerializer.many([album, album]) }
.to raise_error(ArgumentError, 'Serializer instances are reused so they must be stateless. Use `memo.fetch` for memoization purposes instead. Bad keys: name')
.to raise_error(ArgumentError, 'Serializer instances are reused so they must be stateless. Use `memo.fetch` for memoization purposes instead. Bad keys: name in StatefulSerializer')
end

it 'should fail early when `attributes` is used instead of `serializer_attributes`' do
Expand Down

0 comments on commit 8da9ef9

Please sign in to comment.