From fa174bba9dbdec89500cfd7f322bc503571b73c3 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Tue, 15 Aug 2017 18:30:16 +0200 Subject: [PATCH] Freeze the raw object along the wrapper for all fields --- lib/rackstash/fields/abstract_collection.rb | 10 ++++++++++ spec/rackstash/fields/abstract_collection_spec.rb | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/rackstash/fields/abstract_collection.rb b/lib/rackstash/fields/abstract_collection.rb index 920b3f4..c1be8da 100644 --- a/lib/rackstash/fields/abstract_collection.rb +++ b/lib/rackstash/fields/abstract_collection.rb @@ -29,6 +29,16 @@ module Rackstash end alias eql? == + # Prevents further modifications to `self`. A `RuntimeError` will be + # raised if modification is attempted. There is no way to unfreeze a + # frozen object. + # + # @return [self] + def freeze + raw.freeze + super + end + # Compute a hash-code for this collection. # # Two collections with the same raw content will have the same hash code diff --git a/spec/rackstash/fields/abstract_collection_spec.rb b/spec/rackstash/fields/abstract_collection_spec.rb index ca63d86..71cb051 100644 --- a/spec/rackstash/fields/abstract_collection_spec.rb +++ b/spec/rackstash/fields/abstract_collection_spec.rb @@ -92,6 +92,20 @@ describe Rackstash::Fields::AbstractCollection do end end + describe '#freeze' do + it 'freezes the collection and the raw data' do + raw = [123, 'foo'] + collection.send(:raw=, raw) + + expect(raw).not_to be_frozen + + collection.freeze + + expect(collection).to be_frozen + expect(raw).to be_frozen + end + end + describe '#hash' do it 'returns the same hash for the same raw content' do collection.send(:raw=, [123, 'foo'])