From b2c1a1da0ecca8e2151fdebe766b6a3dbfeaba47 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Sat, 6 May 2017 14:47:38 +0200 Subject: [PATCH] Rename Target(List) to Flow(s) Since a flow will significantly affect the representation of a log event over its life before being persistet by the final adapter, the new term better reflects what it is responsible for. It also helps to avoid the dupliction of meaning between Sink, Target, Adapter. --- lib/rackstash/{target.rb => flow.rb} | 2 +- lib/rackstash/flows.rb | 57 ++++++++++ lib/rackstash/target_list.rb | 50 --------- spec/rackstash/flows_spec.rb | 162 +++++++++++++++++++++++++++ spec/rackstash/target_list_spec.rb | 162 --------------------------- 5 files changed, 220 insertions(+), 213 deletions(-) rename lib/rackstash/{target.rb => flow.rb} (94%) create mode 100644 lib/rackstash/flows.rb delete mode 100644 lib/rackstash/target_list.rb create mode 100644 spec/rackstash/flows_spec.rb delete mode 100644 spec/rackstash/target_list_spec.rb diff --git a/lib/rackstash/target.rb b/lib/rackstash/flow.rb similarity index 94% rename from lib/rackstash/target.rb rename to lib/rackstash/flow.rb index c04f363..783a311 100644 --- a/lib/rackstash/target.rb +++ b/lib/rackstash/flow.rb @@ -4,7 +4,7 @@ # of the MIT license. See the LICENSE.txt file for details. module Rackstash - class Target + class Flow def initialize(adapter = nil) @adapter = adapter end diff --git a/lib/rackstash/flows.rb b/lib/rackstash/flows.rb new file mode 100644 index 0000000..9ed45be --- /dev/null +++ b/lib/rackstash/flows.rb @@ -0,0 +1,57 @@ +# Copyright 2017 Holger Just +# +# This software may be modified and distributed under the terms +# of the MIT license. See the LICENSE.txt file for details. + +require 'rackstash/flow' + +module Rackstash + class Flows + def initialize(*flows) + @flows = Concurrent::Array.new + + flows.flatten.each do |flow| + add(flow) + end + end + + def <<(flow) + flow = Flow.new(flow) unless flow.is_a?(Flow) + @flows << flow + self + end + alias add << + + def [](index) + @flows[index] + end + + def []=(index, flow) + flow = Flow.new(flow) unless flow.is_a?(Flow) + @flows[index] = flow + end + + def empty? + @flows.empty? + end + + def inspect + id_str = (object_id << 1).to_s(16).rjust(DEFAULT_OBJ_ID_STR_WIDTH, '0') + "#<#{self.class.name}:0x#{id_str} #{self}>" + end + + def length + @flows.length + end + alias size length + + def to_ary + @flows.to_a + end + alias to_a to_ary + + def to_s + @flows.to_s + end + end +end diff --git a/lib/rackstash/target_list.rb b/lib/rackstash/target_list.rb deleted file mode 100644 index 9bde06f..0000000 --- a/lib/rackstash/target_list.rb +++ /dev/null @@ -1,50 +0,0 @@ -module Rackstash - class TargetList - def initialize(*targets) - @targets = Concurrent::Array.new - - targets.flatten.each do |target| - add(target) - end - end - - def <<(target) - target = Target.new(target) unless target.is_a?(Target) - @targets << target - self - end - alias add << - - def [](index) - @targets[index] - end - - def []=(index, target) - target = Target.new(target) unless target.is_a?(Target) - @targets[index] = target - end - - def empty? - @targets.empty? - end - - def inspect - id_str = (object_id << 1).to_s(16).rjust(DEFAULT_OBJ_ID_STR_WIDTH, '0') - "#<#{self.class.name}:0x#{id_str} #{self}>" - end - - def length - @targets.length - end - alias size length - - def to_ary - @targets.to_a - end - alias to_a to_ary - - def to_s - @targets.to_s - end - end -end diff --git a/spec/rackstash/flows_spec.rb b/spec/rackstash/flows_spec.rb new file mode 100644 index 0000000..819dc3b --- /dev/null +++ b/spec/rackstash/flows_spec.rb @@ -0,0 +1,162 @@ +# Copyright 2017 Holger Just +# +# This software may be modified and distributed under the terms +# of the MIT license. See the LICENSE.txt file for details. + +require 'spec_helper' + +require 'rackstash/flows' +require 'rackstash/flow' + +describe Rackstash::Flows do + let(:flows) { Rackstash::Flows.new } + + def a_flow + flow = instance_double('Rackstash::Flow') + allow(flow).to receive(:is_a?).with(Rackstash::Flow).and_return(true) + flow + end + + describe '#initialize' do + it 'accepts a single flow' do + list = Rackstash::Flows.new(a_flow) + expect(list.size).to eql 1 + end + + it 'accepts a list of flows' do + flows = 3.times.map { a_flow } + + list_with_array = Rackstash::Flows.new(flows) + expect(list_with_array.size).to eql 3 + + list_with_splat = Rackstash::Flows.new(*flows) + expect(list_with_splat.size).to eql 3 + end + end + + describe '#<<' do + let(:flow) { a_flow } + + it 'adds a new flow at the end of the list' do + expect(flows.size).to eql 0 + flows << flow + expect(flows.size).to eql 1 + expect(flows[0]).to equal flow + end + + it 'tries to find a matching flow' do + wrapped = Object.new + flow = Object.new + + flow_class = class_double('Rackstash::Flow').as_stubbed_const + expect(flow_class).to receive(:new).with(wrapped).and_return(flow) + + expect(flows.size).to eql 0 + flows << wrapped + expect(flows.size).to eql 1 + expect(flows[0]).to equal flow + end + + it 'can use the #add alias' do + expect(flows.size).to eql 0 + flows.add flow + expect(flows.size).to eql 1 + expect(flows[0]).to equal flow + end + end + + describe '#[]' do + let(:flow) { a_flow } + + it 'returns the index flow' do + flows << flow + expect(flows[0]).to equal flow + expect(flows[1]).to be_nil + end + end + + describe '#[]=' do + it 'sets a flow' do + original_flow = a_flow + new_flow = a_flow + + flows << original_flow + expect(flows[0]).to equal original_flow + + flows[0] = new_flow + expect(flows[0]).to equal new_flow + end + + it 'adds nil flows if necessary' do + flow = a_flow + flows[3] = flow + expect(flows.to_a).to eql [nil, nil, nil, flow] + end + + it 'tries to find a matching flow' do + wrapped = Object.new + flow = Object.new + + flow_class = class_double('Rackstash::Flow').as_stubbed_const + expect(flow_class).to receive(:new).with(wrapped).and_return(flow) + + flows[0] = wrapped + expect(flows[0]).to equal flow + end + end + + describe '#empty?' do + it 'is true if empty' do + expect(flows).to be_empty + flows << a_flow + expect(flows).not_to be_empty + end + end + + describe '#inspect' do + it 'formats the object' do + expect(flows).to receive(:to_s).and_return('[""]') + expect(flows.inspect).to( + match %r{\A#"\]>\z} + ) + end + end + + describe '#length' do + it 'returns the number of flows' do + expect { flows << a_flow} + .to change { flows.length }.from(0).to(1) + end + + it 'can use size alias' do + expect { flows << a_flow} + .to change { flows.size }.from(0).to(1) + end + end + + describe '#to_ary' do + it 'returns an array' do + flows << a_flow + + expect(flows.to_a).to be_an_instance_of(::Array) + expect(flows.to_a).not_to be_empty + end + + it 'returns a new object each time' do + array = flows.to_a + expect(flows.to_a).to eql array + expect(flows.to_a).not_to equal array + + array << a_flow + expect(flows.to_a).not_to eql array + end + end + + describe '#to_s' do + it 'returns the array representation' do + flows << a_flow + + expect(flows.to_s).to eql flows.to_a.to_s + end + end +end diff --git a/spec/rackstash/target_list_spec.rb b/spec/rackstash/target_list_spec.rb deleted file mode 100644 index 89fed0c..0000000 --- a/spec/rackstash/target_list_spec.rb +++ /dev/null @@ -1,162 +0,0 @@ -# Copyright 2017 Holger Just -# -# This software may be modified and distributed under the terms -# of the MIT license. See the LICENSE.txt file for details. - -require 'spec_helper' - -require 'rackstash/target_list' -require 'rackstash/target' - -describe Rackstash::TargetList do - let(:target_list) { Rackstash::TargetList.new } - - def a_target - target = instance_double('Rackstash::Target') - allow(target).to receive(:is_a?).with(Rackstash::Target).and_return(true) - target - end - - describe '#initialize' do - it 'accepts a single target' do - list = Rackstash::TargetList.new(a_target) - expect(list.size).to eql 1 - end - - it 'accepts a list of targets' do - targets = 3.times.map { a_target } - - list_with_array = Rackstash::TargetList.new(targets) - expect(list_with_array.size).to eql 3 - - list_with_splat = Rackstash::TargetList.new(*targets) - expect(list_with_splat.size).to eql 3 - end - end - - describe '#<<' do - let(:target) { a_target } - - it 'adds a new target at the end of the list' do - expect(target_list.size).to eql 0 - target_list << target - expect(target_list.size).to eql 1 - expect(target_list[0]).to equal target - end - - it 'tries to find a matching target' do - wrapped = Object.new - target = Object.new - - target_class = class_double('Rackstash::Target').as_stubbed_const - expect(target_class).to receive(:new).with(wrapped).and_return(target) - - expect(target_list.size).to eql 0 - target_list << wrapped - expect(target_list.size).to eql 1 - expect(target_list[0]).to equal target - end - - it 'can use the #add alias' do - expect(target_list.size).to eql 0 - target_list.add target - expect(target_list.size).to eql 1 - expect(target_list[0]).to equal target - end - end - - describe '#[]' do - let(:target) { a_target } - - it 'returns the index target' do - target_list << target - expect(target_list[0]).to equal target - expect(target_list[1]).to be_nil - end - end - - describe '#[]=' do - it 'sets a target' do - original_target = a_target - new_target = a_target - - target_list << original_target - expect(target_list[0]).to equal original_target - - target_list[0] = new_target - expect(target_list[0]).to equal new_target - end - - it 'adds nil targets if necessary' do - target = a_target - target_list[3] = target - expect(target_list.to_a).to eql [nil, nil, nil, target] - end - - it 'tries to find a matching target' do - wrapped = Object.new - target = Object.new - - target_class = class_double('Rackstash::Target').as_stubbed_const - expect(target_class).to receive(:new).with(wrapped).and_return(target) - - target_list[0] = wrapped - expect(target_list[0]).to equal target - end - end - - describe '#empty?' do - it 'is true if empty' do - expect(target_list).to be_empty - target_list << a_target - expect(target_list).not_to be_empty - end - end - - describe '#inspect' do - it 'formats the object' do - expect(target_list).to receive(:to_s).and_return('[""]') - expect(target_list.inspect).to( - match %r{\A#"\]>\z} - ) - end - end - - describe '#length' do - it 'returns the number of targets' do - expect { target_list << a_target} - .to change { target_list.length }.from(0).to(1) - end - - it 'can use size alias' do - expect { target_list << a_target} - .to change { target_list.size }.from(0).to(1) - end - end - - describe '#to_ary' do - it 'returns an array' do - target_list << a_target - - expect(target_list.to_a).to be_an_instance_of(::Array) - expect(target_list.to_a).not_to be_empty - end - - it 'returns a new object each time' do - array = target_list.to_a - expect(target_list.to_a).to eql array - expect(target_list.to_a).not_to equal array - - array << a_target - expect(target_list.to_a).not_to eql array - end - end - - describe '#to_s' do - it 'returns the array representation' do - target_list << a_target - - expect(target_list.to_s).to eql target_list.to_a.to_s - end - end -end