class Gamefic::Messenger
Message formatting and buffering.
Public Class Methods
Public Instance Methods
Source
# File lib/gamefic/messenger.rb, line 15 def buffer @buffers.push('') yield if block_given? @buffers.pop end
Create a temporary buffer while yielding the given block and return the buffered text.
@return [String]
Source
# File lib/gamefic/messenger.rb, line 54 def flush @buffers.pop.tap { @buffers.push '' } end
Clear the current buffer.
@return [String] The flushed message
Source
# File lib/gamefic/messenger.rb, line 58 def format(message) "<p>#{message.strip}</p>" .gsub(/[ \t\r]*\n[ \t\r]*\n[ \t\r]*/, "</p><p>") .gsub(/[ \t]*\n[ \t]*/, ' ') .gsub(/<p>\s*<p>/, '<p>') .gsub(%r{</p>\s*</p>}, '</p>') end
Source
# File lib/gamefic/messenger.rb, line 47 def messages @buffers.last end
Get the currently buffered messages.
@return [String]
Source
# File lib/gamefic/messenger.rb, line 39 def stream(message) @buffers.push(@buffers.pop + message.to_s) .last end
Add a raw text message to the current buffer.
Unlike tell
, this method will not wrap the message in HTML paragraphs.
@param message [String, to_s] @return [String] The messages in the current buffer
Source
# File lib/gamefic/messenger.rb, line 28 def tell(message) @buffers.push(@buffers.pop + format(message.to_s)) .last end
Add a formatted message to the current buffer.
This method will automatically wrap the message in HTML paragraphs. To send a message without formatting, use stream
instead.
@param message [String, to_s] @return [String] The messages in the current buffer