1
0
mirror of https://github.com/meineerde/rackstash.git synced 2026-01-31 17:27:13 +00:00

Actually freeze messages after initialization

This commit is contained in:
Holger Just 2017-01-25 18:51:19 +01:00
parent 1fc6a93ce5
commit 2b72a1e5cf
2 changed files with 13 additions and 8 deletions

View File

@ -42,18 +42,19 @@ module Rackstash
@time = dup_freeze(time)
@progname = dup_freeze(progname)
@formatter = formatter
# Freeze the newly created message to ensure it can't be changed.
# All passed values are also effectively frozen, making the Message an
# immutable object.
freeze
end
def severity_label
SEVERITY_LABEL[@severity] || SEVERITY_LABEL.last
end
def frozen?
true
end
def to_s
@to_s ||= @formatter.call(severity_label, @time, @progname, @message)
@formatter.call(severity_label, @time, @progname, @message)
end
alias_method :to_str, :to_s
alias_method :as_json, :to_s

View File

@ -139,7 +139,7 @@ describe Rackstash::Message do
message = 'Hello World'
formatter = double('formatter')
expect(formatter).to receive(:call).once
expect(formatter).to receive(:call)
.with('DEBUG', time, progname, message)
.and_return('Formatted Message')
@ -152,8 +152,12 @@ describe Rackstash::Message do
)
expect(message.to_s).to eql 'Formatted Message'
# Same result, but no additional call to the formatter
expect(message.to_s).to eql 'Formatted Message'
end
end
describe '#frozen?' do
it 'is always true' do
expect(Rackstash::Message.new('Beep boop')).to be_frozen
end
end
end