Skip to content

Commit

Permalink
Merge branch 'fix/correctly-invert-serializers'
Browse files Browse the repository at this point in the history
  • Loading branch information
sorentwo committed Jul 4, 2016
2 parents f070929 + 1c860a9 commit 4aaa51a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/readthis/serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Serializers
}.freeze

# The hard serializer limit, based on the number of possible values within
# a single 8bit integer.
# a single 3bit integer.
SERIALIZER_LIMIT = 7

attr_reader :serializers, :inverted
Expand Down Expand Up @@ -96,7 +96,7 @@ def assoc(serializer)
# serializers.rassoc(1) #=> Marshal
#
def rassoc(flag)
inverted[flag & inverted.length]
inverted[flag & SERIALIZER_LIMIT]
end

# @private
Expand Down
19 changes: 15 additions & 4 deletions spec/readthis/serializers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,29 @@
end

describe '#rassoc' do
it 'inverts the current set of serializers' do
serializers = Readthis::Serializers.new
let(:serializers) { Readthis::Serializers.new }

it 'inverts the current set of serializers' do
expect(serializers.rassoc(1)).to eq(Marshal)
end

it 'returns custom serializers' do
serializers = Readthis::Serializers.new
serializers << CustomSerializer

expect(serializers.rassoc(4)).to eq(CustomSerializer)
end

it 'inverts default serializers after adding custom one' do
serializers << CustomSerializer
expect(serializers.rassoc(1)).to eq(Marshal)
expect(serializers.rassoc(3)).to eq(JSON)
end

it 'takes into account only first 3 bytes of passed integer' do
expect(serializers.rassoc(1)).to eq(Marshal)
expect(serializers.rassoc(11)).to eq(JSON)
serializers << CustomSerializer
expect(serializers.rassoc(12)).to eq(CustomSerializer)
end
end

describe '#freeze!' do
Expand Down

0 comments on commit 4aaa51a

Please sign in to comment.