From 6ba3bf1401f8e02319a4b336672c49c1878333aa Mon Sep 17 00:00:00 2001 From: Marvin Ahlgrimm Date: Tue, 27 Aug 2024 19:54:59 +0200 Subject: [PATCH] Accept block for stream initialization --- spec/marten-turbo/turbo_stream_spec.cr | 10 ++++++++++ src/marten_turbo/turbo_stream.cr | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/spec/marten-turbo/turbo_stream_spec.cr b/spec/marten-turbo/turbo_stream_spec.cr index ee83e8b..270084d 100644 --- a/spec/marten-turbo/turbo_stream_spec.cr +++ b/spec/marten-turbo/turbo_stream_spec.cr @@ -6,6 +6,16 @@ describe MartenTurbo::TurboStream do stream = MartenTurbo::TurboStream.new stream.to_s.should eq "" end + + it "accepts a block to initialize the stream" do + stream = MartenTurbo::TurboStream.new do |stream| + stream.append("messages", "
Message 1
") + end + + stream.to_s.should contain "" + stream.to_s.should contain "
Message 1
" + stream.to_s.should contain "
" + end end describe "#append" do diff --git a/src/marten_turbo/turbo_stream.cr b/src/marten_turbo/turbo_stream.cr index 55f5c28..a27a7b4 100644 --- a/src/marten_turbo/turbo_stream.cr +++ b/src/marten_turbo/turbo_stream.cr @@ -8,6 +8,11 @@ module MartenTurbo @streams = [] of String end + def initialize + @streams = [] of String + yield self + end + # Creates a new TurboStream instance and adds a single action. # # ```