diff --git a/lib/rackstash/flows.rb b/lib/rackstash/flows.rb index 4c43ef4..6ca3556 100644 --- a/lib/rackstash/flows.rb +++ b/lib/rackstash/flows.rb @@ -103,6 +103,16 @@ module Rackstash end end + # Prevents further modifications to the flows list. A `RuntimeError` will be + # raised if you attempt to add, delete, or overwrite flows in the list. + # There is no way to unfreeze a frozen object. + # + # @return [self] + def freeze + @flows.freeze + super + end + # @return [String] a string representation of `self` def inspect id_str = Object.instance_method(:to_s).bind(self).call[2..-2] @@ -192,8 +202,17 @@ module Rackstash @flows.to_s end + protected + + attr_reader :flows + private + def initialize_copy(other) + @flows = other.flows.dup + super + end + # Create a deep duplicate of an event hash. It is assumed that the input # event follows the normalized structure as generated by # {Fields::Hash#to_h}. diff --git a/spec/rackstash/flows_spec.rb b/spec/rackstash/flows_spec.rb index 9511d4d..6b93e7b 100644 --- a/spec/rackstash/flows_spec.rb +++ b/spec/rackstash/flows_spec.rb @@ -122,6 +122,41 @@ describe Rackstash::Flows do end end + describe '#dup' do + it 'dups the flows' do + flows << a_flow + copied = flows.dup + + expect(copied).to be_instance_of described_class + expect(copied.length).to eql 1 + expect(copied).to_not equal flows + end + + it 'separates the flows' do + flows << a_flow + + copied = flows.dup + copied << a_flow + + expect(copied.first).to equal flows.first + + expect(flows.length).to eql 1 + expect(copied.length).to eql 2 + end + end + + describe '#freeze' do + it 'freezes the object' do + expect(flows.freeze).to equal flows + expect(flows).to be_frozen + end + + it 'denies all further changes' do + flows.freeze + expect { flows << a_flow }.to raise_error(RuntimeError) + end + end + describe '#reopen' do it 'calls reopen on all flows' do [a_flow, a_flow].each do |flow|