From f2cd5df5542858545f201e3549ea3beef77cb42a Mon Sep 17 00:00:00 2001 From: Holger Just Date: Thu, 2 Feb 2017 21:50:04 +0100 Subject: [PATCH] Separate initialize_dup and initialize_copy in fields --- lib/rackstash/fields/abstract_collection.rb | 8 +++- lib/rackstash/fields/tags.rb | 26 +++++++--- spec/rackstash/fields/tags_spec.rb | 53 ++++++++++++++++++--- 3 files changed, 73 insertions(+), 14 deletions(-) diff --git a/lib/rackstash/fields/abstract_collection.rb b/lib/rackstash/fields/abstract_collection.rb index fbb4bec..afbf650 100644 --- a/lib/rackstash/fields/abstract_collection.rb +++ b/lib/rackstash/fields/abstract_collection.rb @@ -41,12 +41,18 @@ module Rackstash private - def initialize_copy(source) + def initialize_dup(source) super self.raw = source.raw == nil ? nil : source.raw.dup self end + def initialize_clone(source) + super + self.raw = source.raw == nil ? nil : source.raw.clone + self + end + def new(raw) self.class.new.tap do |new_object| new_object.raw = raw diff --git a/lib/rackstash/fields/tags.rb b/lib/rackstash/fields/tags.rb index d5edbb5..0a57d81 100644 --- a/lib/rackstash/fields/tags.rb +++ b/lib/rackstash/fields/tags.rb @@ -15,7 +15,10 @@ module Rackstash end def <<(tag) - merge!(tag) + tag = resolve_value(tag) + tag = utf8_encode(tag).freeze + @raw << tag + self end def as_json(*) @@ -33,12 +36,12 @@ module Rackstash @raw.empty? end - def merge(*tags, scope: nil) - dup.merge!(*tags, scope: scope) + def merge(tags, scope: nil) + dup.merge!(tags, scope: scope) end - def merge!(*tags, scope: nil) - @raw.merge normalize_tags(tags.to_ary) + def merge!(tags, scope: nil) + @raw.merge normalize_tags(tags) self end @@ -55,7 +58,13 @@ module Rackstash def normalize_tags(value, scope: nil) value = resolve_value(value, scope: scope) - if value.respond_to?(:to_ary) + if value.is_a?(self.class) + value.to_a + elsif value.is_a?(Set) + value = value.map { |tag| normalize_tags(tag) } + value.flatten! + value + elsif value.respond_to?(:to_ary) value = value.to_ary.map { |tag| normalize_tags(tag) } value.flatten! value @@ -64,6 +73,11 @@ module Rackstash end end end + + # param tags [Set, Array] + def self.Tags(tags) + Rackstash::Fields::Tags.new.merge!(tags) + end end end diff --git a/spec/rackstash/fields/tags_spec.rb b/spec/rackstash/fields/tags_spec.rb index 9e9c4bd..904475b 100644 --- a/spec/rackstash/fields/tags_spec.rb +++ b/spec/rackstash/fields/tags_spec.rb @@ -12,15 +12,18 @@ describe Rackstash::Fields::Tags do describe '#<<' do it 'calls merge!' do - expect(tags).to receive(:merge!).with('tag').and_call_original tags << 'tag' expect(tags.tagged?('tag')).to be true end + + it 'returns tags' do + expect(tags << 'tag').to equal tags + end end describe '#as_json' do before do - tags.merge!(123, 'tag', true) + tags.merge! [123, 'tag', true] end it 'returns a simple array' do @@ -67,7 +70,7 @@ describe Rackstash::Fields::Tags do describe '#merge' do it 'returns a new object' do - new_tags = tags.merge('hello') + new_tags = tags.merge ['hello'] expect(new_tags).to be_a Rackstash::Fields::Tags expect(new_tags.tagged?('hello')).to be true @@ -80,19 +83,45 @@ describe Rackstash::Fields::Tags do describe '#merge!' do it 'merges multiple tags as strings' do - tags.merge! 'foo', 'bar' + tags.merge! ['foo', 'bar'] expect(tags.to_a).to eql ['foo', 'bar'] - tags.merge! 123, 'foo', nil + tags.merge! [123, 'foo', nil] expect(tags.to_a).to eql ['foo', 'bar', '123', ''] expect(tags.to_a).to all be_frozen end + + it 'resolves procs' do + tags.merge! [-> { 123 }] + expect(tags.tagged?('123')).to be true + end + + it 'flattens arguments' do + tags.merge! [123, [-> { ['foo', -> { 'bar' }] }]] + expect(tags.to_a).to eql ['123', 'foo', 'bar'] + end + + it 'accepts tags' do + new_tags = Rackstash::Fields::Tags.new + new_tags << 'foo' + + tags.merge! [new_tags] + + expect(tags.to_a).to eql ['foo'] + end + + it 'accepts a set' do + new_tags = Set['foo', 'bar'] + tags.merge! [new_tags] + + expect(tags.to_a).to eql ['foo', 'bar'] + end end describe '#tagged?' do it 'checks is the argument is tagged' do - tags.merge! 'foo', '123' + tags.merge! ['foo', '123'] expect(tags.tagged?('foo')).to be true expect(tags.tagged?(:foo)).to be true @@ -108,11 +137,21 @@ describe Rackstash::Fields::Tags do it 'returns a copy of the internal set' do expect(tags.to_set).to be_a Set - tags.merge! 'foo', nil + tags.merge! ['foo', nil] expect(tags.to_set.include?('foo')).to be true expect(tags.to_set.include?(nil)).to be false expect(tags.to_set.include?('')).to be true end end + + describe 'Converter' do + it 'creates a new tags list' do + raw = [Time.now, 'foo'] + tags = Rackstash::Fields::Tags(raw) + + expect(tags).to be_a Rackstash::Fields::Tags + expect(tags.to_a).to match [String, 'foo'] + end + end end