Sha256: 9beec3b0456fb08c131fca7bda82b932ccdbf1d5247a291b1b59f518f8fd562e

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# A wrapper around a string to indicate it is HTML-safe and
# can be rendered directly without escaping.
class Brut::FrontEnd::Templates::HTMLSafeString
  module Refinement
    refine String do
      def html_safe! = Brut::FrontEnd::Templates::HTMLSafeString.from_string(self)
      def html_safe? = false
    end
  end
  attr_reader :string
  def initialize(string)
    @string = string
  end

  # Wrap a string in an HTMLSafeString if needed.
  def self.from_string(string_or_html_safe_string)
    if string_or_html_safe_string.kind_of?(self)
      string_or_html_safe_string
    else
      self.new(string_or_html_safe_string)
    end
  end

  # This must be convertible to a string
  def to_s       = @string
  def to_str     = @string
  def html_safe! = self
  def html_safe? = true
  def capitalize = self.class.new(@string.capitalize)
  def downcase   = self.class.new(@string.downcase)
  def upcase     = self.class.new(@string.upcase)

  def +(other)
    if other.html_safe?
      self.class.new(@string + other.to_s)
    else
      @string + other.to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
brut-0.0.1 lib/brut/front_end/templates/html_safe_string.rb