From b91a31335a87e59153a18c2a58b1f3332ea517aa Mon Sep 17 00:00:00 2001 From: Holger Just Date: Thu, 2 Feb 2017 21:49:07 +0100 Subject: [PATCH] Add empty? predicate to Fields::Array --- lib/rackstash/fields/array.rb | 4 ++++ lib/rackstash/fields/hash.rb | 4 ++++ spec/rackstash/fields/array_spec.rb | 10 ++++++++++ spec/rackstash/fields/hash_spec.rb | 10 ++++++++++ 4 files changed, 28 insertions(+) diff --git a/lib/rackstash/fields/array.rb b/lib/rackstash/fields/array.rb index cf1743c..750226b 100644 --- a/lib/rackstash/fields/array.rb +++ b/lib/rackstash/fields/array.rb @@ -39,6 +39,10 @@ module Rackstash self end + def empty? + @raw.empty? + end + def length @raw.length end diff --git a/lib/rackstash/fields/hash.rb b/lib/rackstash/fields/hash.rb index d5ccda7..5706fa2 100644 --- a/lib/rackstash/fields/hash.rb +++ b/lib/rackstash/fields/hash.rb @@ -45,6 +45,10 @@ module Rackstash self end + def empty? + @raw.empty? + end + def keys @raw.keys end diff --git a/spec/rackstash/fields/array_spec.rb b/spec/rackstash/fields/array_spec.rb index bb2afd7..4f3dafb 100644 --- a/spec/rackstash/fields/array_spec.rb +++ b/spec/rackstash/fields/array_spec.rb @@ -123,6 +123,16 @@ describe Rackstash::Fields::Array do end end + describe '#empty?' do + it 'returns true of there are any tags' do + expect(array.empty?).to be true + array[0] = 'foo' + expect(array.empty?).to be false + array.clear + expect(array.empty?).to be true + end + end + describe '#length' do it 'returns the length of the array' do expect(array.length).to eql 0 diff --git a/spec/rackstash/fields/hash_spec.rb b/spec/rackstash/fields/hash_spec.rb index e755ea5..4989faa 100644 --- a/spec/rackstash/fields/hash_spec.rb +++ b/spec/rackstash/fields/hash_spec.rb @@ -160,6 +160,16 @@ describe Rackstash::Fields::Hash do end end + describe '#empty?' do + it 'returns true of there are any fields' do + expect(hash.empty?).to be true + hash['key'] = 'foo' + expect(hash.empty?).to be false + hash.clear + expect(hash.empty?).to be true + end + end + describe '#forbidden_key?' do let(:forbidden_keys) { ['forbidden', :foo] }