class Gamefic::Messenger

Message formatting and buffering.

Public Class Methods

new() click to toggle source
# File lib/gamefic/messenger.rb, line 7
def initialize
  @buffers = ['']
end

Public Instance Methods

buffer() { || ... } click to toggle source

Create a temporary buffer while yielding the given block and return the buffered text.

@return [String]

# File lib/gamefic/messenger.rb, line 15
def buffer
  @buffers.push('')
  yield if block_given?
  @buffers.pop
end
flush() click to toggle source

Clear the buffered messages.

@return [String] The flushed message

# File lib/gamefic/messenger.rb, line 54
def flush
  @buffers.pop.tap { @buffers.push '' }
end
format(message) click to toggle 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
messages() click to toggle source

Get the currently buffered messages.

@return [String]

# File lib/gamefic/messenger.rb, line 47
def messages
  @buffers.last
end
stream(message) click to toggle source

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

# File lib/gamefic/messenger.rb, line 39
def stream(message)
  @buffers.push(@buffers.pop + message.to_s)
          .last
end
tell(message) click to toggle source

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

# File lib/gamefic/messenger.rb, line 28
def tell(message)
  @buffers.push(@buffers.pop + format(message.to_s))
          .last
end