Class: Padrino::SafeBuffer

Inherits:
String show all
Defined in:
padrino-helpers/lib/padrino/safe_buffer.rb

Overview

Padrino::SafeBuffer is based on ActiveSupport::SafeBuffer

Defined Under Namespace

Classes: SafeConcatError

Constant Summary collapse

UNSAFE_STRING_METHODS =
%w[
  capitalize chomp chop delete downcase gsub lstrip next reverse rstrip
  slice squeeze strip sub succ swapcase tr tr_s upcase
]

Instance Method Summary collapse

Methods inherited from String

#camelize, #classify, #colorize, #constantize, #html_safe, #pluralize, #underscore

Constructor Details

#initialize(str = '') ⇒ SafeBuffer

Returns a new instance of SafeBuffer.



37
38
39
40
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 37

def initialize(str = '')
  @html_safe = true
  super
end

Instance Method Details

#%(other) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 64

def %(other)
  escaped_args =
    case other
    when Hash
      other.transform_values { |arg| html_escape_interpolated_argument(arg) }
    else
      Array(other).map { |arg| html_escape_interpolated_argument(arg) }
    end

  self.class.new(super(escaped_args))
end

#+(other) ⇒ Object



60
61
62
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 60

def +(other)
  dup.concat(other)
end

#[](*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 20

def [](*args)
  if args.size < 2
    super
  elsif html_safe?
    new_safe_buffer = super
    new_safe_buffer&.instance_variable_set :@html_safe, true
    new_safe_buffer
  else
    to_str[*args]
  end
end

#clone_emptyObject



47
48
49
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 47

def clone_empty
  self[0, 0]
end

#concat(value) ⇒ Object Also known as: <<



51
52
53
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 51

def concat(value)
  super(html_escape_interpolated_argument(value))
end

#encode_with(coder) ⇒ Object



88
89
90
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 88

def encode_with(coder)
  coder.represent_object nil, to_str
end

#html_safe?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 76

def html_safe?
  defined?(@html_safe) && @html_safe
end

#initialize_copy(other) ⇒ Object



42
43
44
45
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 42

def initialize_copy(other)
  super
  @html_safe = other.html_safe?
end

#prepend(value) ⇒ Object



56
57
58
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 56

def prepend(value)
  super(html_escape_interpolated_argument(value))
end

#safe_concat(value) ⇒ Object

Raises:



32
33
34
35
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 32

def safe_concat(value)
  raise SafeConcatError unless html_safe?
  original_concat(value)
end

#to_paramObject



84
85
86
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 84

def to_param
  to_str
end

#to_sObject



80
81
82
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 80

def to_s
  self
end