Skip to content

Commit

Permalink
91 allow includes strings (#93)
Browse files Browse the repository at this point in the history
* add hash benchmarking to performance tests

* Add missing attribute in README example

* Disable GC before doing performance test

* Enable oj to AM for fair benchmark test

* add information on performance methodology

* add oss metadata

* Make an error that demonstrates [Issue

* Simple RSpec test that fails with a non-empty string but passes with a
non-empty symbol
* To run the test, rspec spec/lib/object_serializer_spec.rb

* Map includes to symbols if they are provided as strings

* Includes would fail with an ArgumentError unless they were explicitly
provided as symbols (see #97)
* This is solved by mapping the strings to symbols in the
ObjectSerializer initializer
* No real impact on performance here
  • Loading branch information
sjmog authored and andyjeffries committed May 25, 2018
1 parent cd1bc09 commit fc9cc41
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/fast_jsonapi/object_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ def process_options(options)

if options[:include].present?
@includes = options[:include].delete_if(&:blank?).map(&:to_sym)
self.class.validate_includes!(@includes)
validate_includes!(@includes)
end
end

def validate_includes!(includes)
return if includes.blank?

existing_relationships = self.class.relationships_to_serialize.keys.to_set

unless existing_relationships.superset?(includes.to_set)
raise ArgumentError, "One of keys from #{includes} is not specified as a relationship on the serializer"
end
end

Expand Down
3 changes: 3 additions & 0 deletions spec/lib/object_serializer_performance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
}
}

before(:all) { GC.disable }
after(:all) { GC.enable }

context 'when testing performance of serialization' do
it 'should create a hash of 1000 records in less than 50 ms' do
movies = 1000.times.map { |_i| movie }
Expand Down

0 comments on commit fc9cc41

Please sign in to comment.