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

Add to_json method to messages, don't freeze it anymore

This commit is contained in:
Holger Just 2017-01-29 16:15:34 +01:00
parent 52268c2a35
commit 5d1890bb5b
2 changed files with 25 additions and 11 deletions

View File

@ -42,11 +42,6 @@ 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
@ -56,8 +51,14 @@ module Rackstash
def to_s
cleanup @formatter.call(severity_label, @time, @progname, @message)
end
alias_method :to_str, :to_s
alias_method :as_json, :to_s
alias :as_json :to_s
# Messages are implicitly conversible to Strings
alias :to_str :to_s
def to_json
as_json.to_json
end
private

View File

@ -180,11 +180,24 @@ describe Rackstash::Message do
expect(message.to_s).to eql '#<StandardError: An error>'
end
end
it 'is aliased to to_str' do
message = Rackstash::Message.new('hello world')
expect(message.to_str).to eql 'hello world'
end
describe '#frozen?' do
it 'is always true' do
expect(Rackstash::Message.new('Beep boop')).to be_frozen
it 'is aliased to as_json' do
message = Rackstash::Message.new('hello world')
expect(message.as_json).to eql 'hello world'
end
end
it 'formats the message as JSON' do
message = Rackstash::Message.new('hello world')
as_json = 'hello world'
expect(message).to receive(:as_json).and_return(as_json)
expect(as_json).to receive(:to_json).and_return('"json string"')
expect(message.to_json).to eql '"json string"'
end
end